R-Sys.time计算程序运行时间

R用Sys.time()可以查看当前系统时间
程序开始时记录: timestart<-Sys.time()
程序临结束时记录: timeend<-Sys.time()
程序运行时间: runningtime<-timeend-timestart
函数中输出运行时间: print(runningtime)

举例:
> t1<-Sys.time()
> t1
[1] "2013-10-09 18:48:02 CST"
> t2<-Sys.time()
> t1-t2
Time difference of -14.98186 secs
> df<-t1-t2
> df
Time difference of -14.98186 secs
> str(df)
Class 'difftime' atomic [1:1] -15
  ..- attr(*, "units")= chr "secs"
> df[1]
Time difference of -14.98186 secs
 
[注意]:1)df<-t1-t2 是一个字符串变量,而不是我们通常认为的是一个秒钟为单位的数值型变量!!
2)当t1-t2时间超过1分钟时输出结果会自动显示成minutes,当超过1小时时会自动显示成hours

举例:
> t1
[1] "2013-10-09 18:48:02 CST"
> t2
[1] "2013-10-09 14:48:17 CST"
> t2-t1
Time difference of -3.995996 hours

原文地址:https://www.cnblogs.com/awishfullyway/p/6595776.html