1. R语言运行效率分析(1)

2020-01-31  本文已影响0人  灭绝老衲

测试程序运行所需时间的函数的选择

在R语言中,统计一个程序体运行时间一般采用的函数为Sys.time()或者为proc.time()。不过,这两个函数只能根据时间差判断程序执行一次所用的时间,若要重复多次进行平均时间的统计,则显得无能为力。
在此,我们采用microbenchmark函数包来进行统计程序运行时间。该函数使用很简单,只需要输入待测试代码,并且指定“times=N”,程序就会重复运行代码N次,然后返回运行时间的平均值。默认的话times=100。

注:本研究只从表象上展示运算结果,不进行比如时间复杂度和空间复杂度方面的探讨。想要了解这一部分内容,可以参考以下两篇文章:

生成模拟数据以待测试

#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)生成对应的月份的英文名字,并判断该月份所属的季节

方法1: 采用 for + if 语句实现

1: 自定义函数

# 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)
}

2: 调用函数进行运算

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)

(未完!待续……)

上一篇下一篇

猜你喜欢

热点阅读