Reputation: 5935
I am using the R programming language. I am learning about different types of "times" in computing. There seems to be two types of general times : the time corresponding to the general mathematical complexity of the procedures you are running, and the time corresponding to how long it takes your individual computer to run the procedures.
I came across the Sys.time() command in R over here: https://www.r-bloggers.com/2017/05/5-ways-to-measure-running-time-of-r-code/
Does Sys.time() measure the time required for your individual computer to run the commands? For example, I wrote the code below using Sys.time() to measure the time it takes my personal computer to run the following procedure in R for different numbers of observations.
library(Rtsne)
library(cluster)
library(ggplot2)
library(dplyr)
library(dbscan)
library(plotly)
#generate data
var_1 <- rnorm(1000,1,4)
var_2<-rnorm(1000,10,5)
var_3 <- sample( LETTERS[1:4], 1000, replace=TRUE, prob=c(0.1, 0.2, 0.65, 0.05) )
response_variable <- sample( LETTERS[1:2], 100, replace=TRUE, prob=c(0.4, 0.6) )
#put them into a data frame called "f"
f <- data.frame(var_1, var_2, var_3, response_variable)
#declare var_3 and response_variable as factors
f$response_variable = as.factor(f$response_variable)
f$var_3 = as.factor(f$var_3)
#create id
f$ID <- seq_along(f[,1])
mseDF <- NULL
for (size in c(100,500,1000)) {
start_time <- Sys.time()
#gower distance
gower_dist <- daisy(f[1:size, 1:4],
metric = "gower")
lof <- lof(gower_dist, k=3)
tsne_obj <- Rtsne(gower_dist, is_distance = TRUE)
tsne_data <- tsne_obj$Y %>%
data.frame() %>%
setNames(c("X", "Y")) %>%
mutate(
name = f[1:size,]$ID,
lof=lof,
var1=f[1:size,]$var_1,
var2=f[1:size,]$var_2,
var3=f[1:size,]$var_3
)
p1 <- ggplot(aes(x = X, y = Y, size=lof, key=name, var1=var1,
var2=var2, var3=var3), data = tsne_data) +
geom_point(shape=1, col="red")+
theme_minimal()
ggplotly(p1, tooltip = c("lof", "name", "var1", "var2", "var3"))
end_time <- Sys.time()
(final = end_time - start_time)
print (final)
mseDF <- rbind(mseDF, data.frame(size, final))
}
Is this correct?
Extra: visualize the run times:
plot(mseDF)
lines(mseDF)
Thanks
Upvotes: 0
Views: 299
Reputation: 1445
Another way is to use proc.time; e.g.
> start_time <- proc.time()
> # some computation
> for (i in 1:10000000) i ^ 3
> proc.time() - start_time
user system elapsed
0.70 0.02 0.72
Upvotes: 1
Reputation: 263479
Does Sys.time() measure the time required for your individual computer to run the commands?
No. Sys.time()
returns the current time. Similarly Sys.Date()
returns the current date. You could create an elapsed time function that has code in it by subtracting two calls to Sys.time
. But your code implies that you already know how to do that, so perhaps English is not your first language and that question was a simple error in expression. So maybe "yes", since it appears you would get the elapsed time for a result to appear.
You might want to look at ?system.time
system.time({for(i in 1:10e6){x <- Sys.time()}})
# user system elapsed
# 13.274 0.000 13.273
There are also several benchmark packages (as demonstrated in that blog citation above.). Example: pkg:microbenchmark
Upvotes: 1