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)
(b) y=sin(1:Nsim)
(c) y=sapply(1:Nsim,sin)
using system.time() with Nsim = 104; 105; 106; 107.
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)