Question

In: Statistics and Probability

Compare the execution times of the three equivalent R commands (a) y=c();for (t in 1:Nsim) y[t]=sin(t)...

Compare the execution times of the three equivalent R commands
(a) y=c();for (t in 1:Nsim) y[t]=sin(t)
(b) y=sin(1:Nsim)
(c) y=sapply(1:Nsim,sin)
using system.time() with Nsim = 104; 105; 106; 107.

Solutions

Expert Solution

1)

Nsim = 104

start.time <- Sys.time()

y=c();for (t in 1:Nsim) y[t]=sin(t)

end.time <- Sys.time()

time.taken.a <- end.time - start.time

time.taken.a

Time difference of 0.0003597736 secs

start.time <- Sys.time()

y=sin(1:Nsim)

end.time <- Sys.time()

time.taken.b<- end.time - start.time

time.taken.b

Time difference of 0.0001106262 secs

start.time <- Sys.time()

y=sapply(1:Nsim,sin)

end.time <- Sys.time()

time.taken.c <- end.time - start.time

time.taken.c

Time difference of 0.0003988743 secs

time.taken.c>time.taken.a& time.taken.a>time.taken.b

[1] TRUE

#exection time is for (c) is greater than execution time for (a) and (c)

2)
Nsim = 105
start.time <- Sys.time()
y=c();for (t in 1:Nsim) y[t]=sin(t)
end.time <- Sys.time()
time.taken.a <- end.time - start.time
time.taken.a

Time difference of 0.0003886223 secs

start.time <- Sys.time()
y=sin(1:Nsim)
end.time <- Sys.time()
time.taken.b<- end.time - start.time
time.taken.b

Time difference of 0.0001151562 secs
start.time <- Sys.time()

y=sapply(1:Nsim,sin)
end.time <- Sys.time()
time.taken.c <- end.time - start.time
time.taken.c

Time difference of 0.0004687309 secs

time.taken.c>time.taken.a& time.taken.a>time.taken.b

[1] TRUE

#exection time is for (c) is greater than execution time for (a) and (c)

3)

Nsim = 106

start.time <- Sys.time()

y=c();for (t in 1:Nsim) y[t]=sin(t)

end.time <- Sys.time()

time.taken.a <- end.time - start.time

time.taken.a

Time difference of 0.0004012585 secs

start.time <- Sys.time()

y=sin(1:Nsim)

end.time <- Sys.time()

time.taken.b<- end.time - start.time

time.taken.b

Time difference of 0.0001111031 secs

start.time <- Sys.time()

y=sapply(1:Nsim,sin)

end.time <- Sys.time()

time.taken.c <- end.time - start.time

time.taken.c

Time difference of 0.0005280972 secs

time.taken.c>time.taken.a& time.taken.a>time.taken.b

[1] TRUE

#exection time is for (c) is greater than execution time for (a) and (c)

4)
Nsim = 107
start.time <- Sys.time()
y=c();for (t in 1:Nsim) y[t]=sin(t)
end.time <- Sys.time()
time.taken.a <- end.time - start.time
time.taken.a

Time difference of 0.0003688335 secs

start.time <- Sys.time()
y=sin(1:Nsim)
end.time <- Sys.time()
time.taken.b<- end.time - start.time
time.taken.b

Time difference of 0.0001432896 secs
start.time <- Sys.time()

y=sapply(1:Nsim,sin)
end.time <- Sys.time()
time.taken.c <- end.time - start.time
time.taken.c

Time difference of 0.0003988743 secs

time.taken.c>time.taken.a& time.taken.a>time.taken.b

[1] TRUE

#exection time is for (c) is greater than execution time for (a) and (c)

# From values Nsim = 104; 105; 106; 107 it is conclude that

The execution time required for (c) y=sapply(1:Nsim,sin) is greater than (a) y=c();for (t in 1:Nsim) y[t]=sin(t) is greater than (b) y=sin(1:Nsim)

i.e. The minimum executiion time is required for (b) y=sin(1:Nsim) other than (a) & (c)


Related Solutions

Given the following economy: Y = C(Y - T) + I(r) + G C(Y - T)...
Given the following economy: Y = C(Y - T) + I(r) + G C(Y - T) = a + b(Y - T) I(r) = c - dr M/P = L(r,Y) L(r,Y) = eY - fr i. Solve for Y as a function of r, the exogenous variables G and T, and the model's parameters a, b, c, and d. ii. Solve for r as a function of Y, M, P, and the parameters e and f. iii. Derive the aggregate...
y''(t)+(x+y)^2*y(t)=sin(x*t+y*t)-sin(x*t-y*t), y(0)=0, y'(0)=0, x and y are real numbers
y''(t)+(x+y)^2*y(t)=sin(x*t+y*t)-sin(x*t-y*t), y(0)=0, y'(0)=0, x and y are real numbers
Let r(t) = (cos(πt), sin(πt), 3t). Calculate r'(t), T(t) and evaluate T(1).
Let r(t) = (cos(πt), sin(πt), 3t). Calculate r'(t), T(t) and evaluate T(1).
1.The domain of r(t)=〈sin(t),cos(t),ln(t+1)〉 is Select one: a. (−∞,−1] b. (π,2π) c. (−∞,−2] d. (−π,2π) e....
1.The domain of r(t)=〈sin(t),cos(t),ln(t+1)〉 is Select one: a. (−∞,−1] b. (π,2π) c. (−∞,−2] d. (−π,2π) e. ℝ f. (−1,∞) 2. The limt→πr(t) where r(t)=〈t2,et,cos(t)〉 is Select one: a. 〈π2,eπ,−1〉 b. undefined c. 〈π2,eπ,1〉 d. π2+eπ e. 〈π2,eπ,0〉 3.Let v(t)=〈2t,4t,t2〉. Then the length of v′(t) is Select one: a. 20‾‾‾√+2t b. 20+4t2 c. 20+2t‾‾‾‾‾‾‾√ d. 20+4t2‾‾‾‾‾‾‾‾√ e. 6+2t‾‾‾‾‾‾√ f. 1 4. The curvature of a circle centred at the origin with radius 1/3 is Select one: a. 1 b. 1/3 c....
The cycloid has parametric equations x = a(t + sin t), y = a(1 - cos...
The cycloid has parametric equations x = a(t + sin t), y = a(1 - cos t). Find the length of the arc from t = 0 to t = pi. [ Hint: 1 + cosA = 2 cos2 A/2 ]. and the arc length of a parametric
Compare C++ speed to Python speed. To compare Execution Time of: c++ vs. Py : //...
Compare C++ speed to Python speed. To compare Execution Time of: c++ vs. Py : // this is c++ code // covert this code to python and compare their execution time #include<time.h> #include <iostream> using namespace std; #include <chrono> //for time using namespace std::chrono; void doSomething() //to insert any execution code here to measure its running time { int x=6795; // cout << "Enter 4-digit integer x ?\n"; cin >> x; for (int i = 1000; i < 2000000; i++)...
Given r(t) = <2 cos(t), 2 sin(t), 2t>. • What is the arc length of r(t)...
Given r(t) = <2 cos(t), 2 sin(t), 2t>. • What is the arc length of r(t) from t = 0 to t = 5. SET UP integral but DO NOT evaluate • What is the curvature κ(t)?
Solve y''-y=e^t(2cos(t)-sin(t)) with initial condition y(0)=y'(0)=0
Solve y''-y=e^t(2cos(t)-sin(t)) with initial condition y(0)=y'(0)=0
Find the Fourier series of the function. c. f(t) = sin(3pit), -1</ t </ 1
Find the Fourier series of the function. c. f(t) = sin(3pit), -1</ t </ 1
Consider the parametric equation of a curve: x=cos(t), y= 1- sin(t), 0 ≤ t ≤ π...
Consider the parametric equation of a curve: x=cos(t), y= 1- sin(t), 0 ≤ t ≤ π Part (a): Find the Cartesian equation of the curve by eliminating the parameter. Also, graph the curve and indicate with an arrow the direction in which the curve is traced as the parameter increases. Label any x and y intercepts. Part(b): Find the point (x,y) on the curve with tangent slope 1 and write the equation of the tangent line.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT