-1

I am measuring the execution time of a program written in R by writing Sys.time() at starting and end of the code and calculating the difference. The result I am getting is changing at different instances of time.

Eg: adjacent1. matrix

0 1 1 1 1 1.
1 0 1 1 1 1.
1 1 0 1 1 1.
1 1 1 0 1 1.
1 1 1 1 0 1.
1 1 1 1 1 0.

And R Code is

initial_time=Sys.time().
adjacent1%%adjacent1%%adjacent1.
final_time=Sys.time().
time=final_time-initial_time.
print(time)

When I run the code multiple times, the output is different every time.

Time difference of 0.002844095 secs.
Time difference of 0.003111839 secs.
Time difference of 0.002691031 secs.
Time difference of 0.003143072 secs.

This is a simple example. For more complicated R code, there is significant different in execution time.

Why is it so?

Anuja
  • 27
  • 3

1 Answers1

0

As you did not provide an example, I created some dummy data to work with:

sleep_for_5_secs <- function() { Sys.sleep(5) }

start_time <- Sys.time()
sleep_for_5_secs()
end_time <- Sys.time()

start_time
[1] "2018-07-19 14:21:20 MYT"
end_time
[1] "2018-07-19 14:21:25 MYT"

What the above code did was to measure the system time, wait for 5 seconds, and measure the system time again. As you can see, the Sys.time function worked.

DTYK
  • 1,098
  • 1
  • 8
  • 33