在R语言中,统计一个程序体运行时间通常采用的函数为Sys.time()
或者为proc.time()
。不过,这两个函数只能根据时间差判断程序执行一次所用的时间,若要重复屡次进行平均时间的统计,则显得无能为力。
在此,咱们采用microbenchmark
函数包来进行统计程序运行时间。该函数使用很简单,只须要输入待测试代码,而且指定“times=N”,程序就会重复运行代码N次,而后返回运行时间的平均值。默认的话times=100。html
注:本研究只从表象上展现运算结果,不进行好比时间复杂度和空间复杂度方面的探讨。想要了解这一部份内容,能够参考如下两篇文章:java
#generating n integer data between 1 to 12 month_digital<-function(n){ month_digital<-c() for (i in 1:n){ month_digital[i]<-sample(1:12,1,replace = F) } return(month_digital) }
任务:根据生成的测试数据(1~12)生成对应的月份的英文名字,并判断该月份所属的季节
# digital was translated into month's englishname Month_name_for_if<-function(month){ Month_name<-c() for (i in 1:length(month)){ if (month[i]==1) Month_name[i]<-"Jan" if (month[i]==2) Month_name[i]<-"Feb" if (month[i]==3) Month_name[i]<-"Mar" if (month[i]==4) Month_name[i]<-"Apr" if (month[i]==5) Month_name[i]<-"May" if (month[i]==6) Month_name[i]<-"Jun" if (month[i]==7) Month_name[i]<-"Jul" if (month[i]==8) Month_name[i]<-"Aug" if (month[i]==9) Month_name[i]<-"sep" if (month[i]==10) Month_name[i]<-"Oct" if (month[i]==11) Month_name[i]<-"Nov" if (month[i]==12) Month_name[i]<-"Dec" } return(Month_name) } # digital was translated into season's englishname Season_name_for_if<-function(month){ Season_name<-c() for (i in 1:length(month)){ if (month[i]==1) Season_name[i]<-"Winter" if (month[i]==2) Season_name[i]<-"Winter" if (month[i]==3) Season_name[i]<-"Spring" if (month[i]==4) Season_name[i]<-"Spring" if (month[i]==5) Season_name[i]<-"Spring" if (month[i]==6) Season_name[i]<-"Summer" if (month[i]==7) Season_name[i]<-"Summer" if (month[i]==8) Season_name[i]<-"Summer" if (month[i]==9) Season_name[i]<-"Autumn" if (month[i]==10) Season_name[i]<-"Autumn" if (month[i]==11) Season_name[i]<-"Autumn" if (month[i]==12) Season_name[i]<-"Winter" } return(Season_name) } #generating month and season english result_for_if<-function(n){ month<-month_digital(n) # n months Month_name_for_if<-Month_name_for_if(month)# months' names Season_name_for_if<-Season_name_for_if(month) #seasons' names df<-data.frame(month,Month_name_for_if,Season_name_for_if) return(df) }
month<-month_digital(10) #随机生成10个数据进行测试 microbenchmark::microbenchmark(Month_name_for_if(month)) microbenchmark::microbenchmark(Season_name_for_if(month)) microbenchmark::microbenchmark(result_for_if(month))
Unit: microseconds expr min lq mean median uq max neval Month_name_for_if(month) 16.299 16.6255 325.218 16.831 17.174 30813.88 100 Unit: microseconds expr min lq mean median uq max neval Season_name_for_if(month) 15.818 16.347 322.3124 16.7195 18.312 30467.87 100 Unit: microseconds expr min lq mean median uq max neval result_for_if(month) 846.104 854.45 960.1528 867.8155 881.9025 5631.1 100 There were 50 or more warnings (use warnings() to see the first 50)
(未完!待续……)git