[R] Extracing "Interval of Time" in seconds in R

Mohammad Ehsanul Karim wildscop at yahoo.com
Mon Apr 23 07:53:38 CEST 2007


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



More information about the R-help mailing list