栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > R语言

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

R语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

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

  • https://www.cnblogs.com/purple5252/p/11189791.html
  • http://blog.sina.com.cn/s/blog_4d2fda500102wjay.html
生成模拟数据以待测试
#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)

(未完!待续……)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/855732.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号