1. R語言運行效率分析(1)

測試程序運行所需時間的函數的選擇

在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)生成對應的月份的英文名字,並判斷該月份所屬的季節

方法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)

(未完!待續……)git

相關文章
相關標籤/搜索