Mohammad Ehsanul Karim
2007-Apr-23 05:53 UTC
[R] Extracing "Interval of Time" in seconds in R
Dear List,
I want to let R calculate the time (run-time) it
requires to run a self-written simulation function. I
tried as follows: it
enables me to see the starting and finishing time
points.
#############################
"sim.result" <- function(nsim, ...){
Starting <- date()
... # calculations #
final.result <- ... # Output for display #
cat("# of Iterations used =", nsim, "\n" )
Ending <- date()
cat("Start of Program at", Starting, "\n" )
cat("End of Program at", Ending, "\n" )
return(print(final.result, quote = FALSE))
}
#############################
But how about I want the results in difference of
statring and ending time in seconds only (say, in
output, I need the
function to say that "the program took 597 secornds to
run the whole simulation"), not in all these
format(Sys.time(), "%a
%b %d %H:%M:%S %Y")
Again, I tried in the following way, but it does not
seem to do the trick
> starting<-Sys.time()
> ending<-Sys.time()
> format(diff(ending,starting), "%H:%M:%S")
Error in Ops.POSIXt(lag, differences) : * not defined
for "POSIXt" objects> as.numeric(format(ending, "%H:%M:%S"))
[1] NA
How should I proceed?
Thanks for your valuable time.
Thanks in advance.
Mohammad Ehsanul Karim
Using Windows Xp, R 2.3.1
Institute of Statistical Research and Training,
University of Dhaka
?system.time might be what you're looking for. Ted. Mohammad Ehsanul Karim wrote on 04/23/2007 03:53 PM:> Dear List, > > I want to let R calculate the time (run-time) it > requires to run a self-written simulation function. I > tried as follows: it > > enables me to see the starting and finishing time > points. > > ############################# > "sim.result" <- function(nsim, ...){ > > Starting <- date() > > ... # calculations # > > final.result <- ... # Output for display # > cat("# of Iterations used =", nsim, "\n" ) > > Ending <- date() > > cat("Start of Program at", Starting, "\n" ) > cat("End of Program at", Ending, "\n" ) > > return(print(final.result, quote = FALSE)) > } > ############################# > > But how about I want the results in difference of > statring and ending time in seconds only (say, in > output, I need the > > function to say that "the program took 597 secornds to > run the whole simulation"), not in all these > format(Sys.time(), "%a > > %b %d %H:%M:%S %Y") > > Again, I tried in the following way, but it does not > seem to do the trick > > >> starting<-Sys.time() >> ending<-Sys.time() >> format(diff(ending,starting), "%H:%M:%S") >> > Error in Ops.POSIXt(lag, differences) : * not defined > for "POSIXt" objects > >> as.numeric(format(ending, "%H:%M:%S")) >> > [1] NA > > > How should I proceed? > > > > Thanks for your valuable time. > Thanks in advance. > > > Mohammad Ehsanul Karim > Using Windows Xp, R 2.3.1 > Institute of Statistical Research and Training, > University of Dhaka > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Dr E.A. Catchpole Visiting Fellow Univ of New South Wales at ADFA, Canberra, Australia _ and University of Kent, Canterbury, England 'v' - www.pems.adfa.edu.au/~ecatchpole / \ - fax: +61 2 6268 8786 m m - ph: +61 2 6268 8895