Reputation: 8298
I have the sample code below:
f <- function(x) {
v = 0
start = Sys.time()
for (i in 1:x) {
v = v + 1
}
end = Sys.time()
print(end-start)
print(paste0("f() took: ", end-start))
}
f(10)
The 2 outputs are:
Time difference of 5.960464e-06 secs
"f() took: 5.96046447753906e-06"
My question is why when used with paste0
the output is different.
If one wish to get the units of time do the following:
print(paste0("f() took: ", end-start, " ", units(end-start)))
Upvotes: 1
Views: 81
Reputation: 2717
Have a look at ?print.difftime
:
The print() method calls these “time differences”.
It's an in-built method for print that displays differences in dates in this format.
class(Sys.Date()-Sys.Date()+1)
[1] "difftime"
class(paste(Sys.Date()-Sys.Date()+1))
[1] "character"
paste0("f() took: ", end-start)
becomes a character class and so print will handle it differently
Upvotes: 1