how run a function for specified time in R

 

Questions


I’m trying to get a function to run for a specified amount of time, at the moment I’m trying to use the system.time function. I can’t figure out how to define a new variable that takes on cumulative value the function running, then put it into a while loop.

timer<-(system.time(simulated_results<-replicate(n=1,simulation(J,10000,FALSE,0.1),simplify="vector"))[3])

print(timer)

while(cumsum(timer)<15){
    print(cumsum(timer)) 
    simulated_results<-replicate(n=10000,simulation(J,10000,FALSE,0.1),simplify="vector")
}

I would greatly appreciate any help!!!

 

 

————————————————-

Answer

If you want to run some code for a specified number of seconds, you can try the following :

start <- as.numeric(Sys.time())
duration <- 5
results <- NULL
while(as.numeric(Sys.time())-start < duration) {
  results <- c(results, replicate(...))
}

Of course, you have to change the value of duration (in seconds), and replace replicate(...) with your code.

r

[]

Facebook Comments

Post a comment