get execution time in milliseconds in R
1) Timing is operating-system dependent. On Windows you may only get milliseconds.
2) No need to define tic()
and toc()
, R has system.time()
. Here is an example:
R> system.time(replicate(100, sqrt(seq(1.0, 1.0e6))))
user system elapsed
2.210 0.650 2.867
R>
3) There are excellent add-on packages rbenchmark and microbenchmark.
3.1) rbenchmark is particularly useful for comparison of commands, but can also be used directly:
R> library(rbenchmark)
R> x <- seq(1.0, 1.0e6); benchmark(sqrt(x), log(x))
test replications elapsed relative user.self sys.self user.child sys.child
2 log(x) 100 5.408 2.85835 5.21 0.19 0 0
1 sqrt(x) 100 1.892 1.00000 1.62 0.26 0 0
R>
3.2) microbenchmark excels at highest precision measurements:
R> library(microbenchmark)
R> x <- seq(1.0, 1.0e6); microbenchmark(sqrt(x), log(x))
Unit: nanoseconds
expr min lq median uq max
1 log(x) 50589289 50703132 55283301 55353594 55917216
2 sqrt(x) 15309426 15412135 15452990 20011418 39551819
R>
and this last one, particularly on Linux, already gives you nano-seconds. It can also plot results etc so have a closer look at that package.
Get current time in milliseconds
Sys.time
does not return a "formatted time". It returns a POSIXct
classed object, which is the number of seconds since the Unix epoch. Of course, when you print that object, it returns a formatted time. But how something prints is not what it is.
To get the current time in milliseconds, you just need to convert the output of Sys.time
to numeric, and multiply by 1000.
R> print(as.numeric(Sys.time())*1000, digits=15)
[1] 1476538955719.77
Depending on the API call you want to make, you might need to remove the fractional milliseconds.
R current time in milliseconds
You do get milliseconds by default on all operating systems, and (almost as the effective resolution is just a fraction less) microseconds on Linux and macOS -- but you must enable the printing of it.
Default R behaviour
> options(digits.secs=0)
> Sys.time()
[1] "2022-05-14 13:01:57 CDT"
>
Changed to Six Digits
> options(digits.secs=6)
> Sys.time()
[1] "2022-05-14 13:02:54.038276 CDT"
>
I actually set this in my default ~/.Rprofile
to always have six decimals.
Use milliseconds in variable Time with R
Just as "year-month" is not a date (for lack of a day), "hour-min-ss.subsec" is not a datetime.
But if you are willing / able to prepend a date then the different datetime parsers can help you. My favourite is the one I wrote to not require formats. A quick demo:
> tstr <- "00:31:00.0" # added leading zero
> fullstr <- paste("2017-10-17", tstr)
> anytime::anytime(fullstr)
[1] "2017-10-17 00:31:00 CDT"
>
> tstr <- "00:31:00.012345"
> fullstr <- paste("2017-10-17", tstr)
> anytime::anytime(fullstr)
[1] "2017-10-17 00:31:00.012345 CDT"
>
Calculating time intervals in R
It is better to be explicit with the format otherwise you have to read really carefully what happens in the default option:
mydf <- read.table(text="c_time
29/07/12 20:05:01
29/07/12 20:20:59
30/07/12 02:42:08
30/07/12 02:53:17
30/07/12 02:53:18
30/07/12 02:53:19", sep=",", row.names=NULL, header=T)
c_time <- as.POSIXlt( mydf$c_time, format="%d/%m/%y %H:%M:%S")
c_time <- rev( c_time )
difftime(c_time[1:(length(c_time)-1)] , c_time[2:length(c_time)])
##Time differences in secs
##[1] 1 1 669 22869 958
Units of time calculation in R
I think the easiest is to use difftime
with the unit you want from c("auto", "secs", "mins", "hours", " days", "weeks")
For seconds use:
difftime(data$readdate[i+1], data$readdate[i], units="secs")
Related Topics
Change the Color of Action Button in Shiny
Label Minimum and Maximum of Scale Fill Gradient Legend with Text: Ggplot2
How to Append a Plot to an Existing PDF File
How to Prevent Exposure of My Password When Using Rgoogledocs
Understanding Color Scales in Ggplot2
R Change All Columns of Type Factor to Numeric
Convert and Save Distance Matrix to a Specific Format
How to Add Rmse, Slope, Intercept, R^2 to R Plot
Change Background Color of R Plot
How to Jitter Text to Avoid Overlap in a Ggplot2 Scatterplot
Adding Simple Legend to Plot in R
Generate a Sequence of Characters from 'A'-'Z'
How to Get Code Completion for R in Emacs Ess Similar to What Is Available in Rstudio