以上用到了9個方法實現一個問題,在實現的過程當中試驗數據量爲n=10。獲得不一樣方法所用的平均耗時間大小。每種方法在計算平均耗時的重複次數爲N =100。固然上述的每一個方法測試的數據量儘管相同,但因爲(1)數據內容不盡相同,(2)因爲測試耗時的時候後臺打開的程序多少不一樣(CPU和內存任務量不一樣),(3)每種方法所處理的內容不盡相同。這些都對所測試的結果產生影響。爲此,爲了減少這些影響,本節主要經過 增長數據量大小(n)(也能夠增長重複次數(N ),本例沒加以討論) 來估測每種方法的優劣。另外,爲了具備可比性,如下統計結果均爲處理單個數據所消耗的時間。時間單位爲微秒(microsecond)
計算這9個函數處理n個數據分別所用的平均時間(N爲重複次數)java
#n爲隨機化月份數據向量的長度,N爲計算每一個函數平均重複的次數 methods_time<-function(n,N){ month<-month_digital(n) Month_for_if <-microbenchmark(Month_name_for_if (month),times=N,unit="us")#milliseconds Month_for_if_else <-microbenchmark(Month_name_for_if_else (month),times=N,unit="us") Month_for_ifelse <-microbenchmark(Month_name_for_ifelse (month),times=N,unit="us") Month_for_switch <-microbenchmark(Month_name_for_switch (month),times=N,unit="us") Month_which <-microbenchmark(Month_name_which (month),times=N,unit="us") Month_join <-microbenchmark(Month_name_join (month),times=N,unit="us") Month_ddply <-microbenchmark(Month_name_ddply (month),times=N,unit="us") Month_str_replace_all<-microbenchmark(Month_name_str_replace_all(month),times=N,unit="us") Season_for_if <-microbenchmark(Season_name_for_if (month),times=N,unit="us") Season_for_if_else <-microbenchmark(Season_name_for_if_else (month),times=N,unit="us") Season_for_ifelse <-microbenchmark(Season_name_for_ifelse (month),times=N,unit="us") Season_for_switch <-microbenchmark(Season_name_for_switch (month),times=N,unit="us") Season_which <-microbenchmark(Season_name_which (month),times=N,unit="us") Season_join <-microbenchmark(Season_name_join (month),times=N,unit="us") Season_ddply <-microbenchmark(Season_name_ddply (month),times=N,unit="us") Season_str_replace_all<-microbenchmark(Season_name_str_replace_all(month),times=N,unit="us") result_for_if <-microbenchmark(result_for_if (month),times=N,unit="us") result_for_if_else <-microbenchmark(result_for_if_else (month),times=N,unit="us") result_for_ifelse <-microbenchmark(result_for_ifelse (month),times=N,unit="us") result_for_switch <-microbenchmark(result_for_switch (month),times=N,unit="us") result_which <-microbenchmark(result_which (month),times=N,unit="us") result_join <-microbenchmark(result_join (month),times=N,unit="us") result_ddply <-microbenchmark(result_ddply (month),times=N,unit="us") result_str_replace_all<-microbenchmark(result_str_replace_all(month),times=N,unit="us") Month<-c(summary(Month_for_if)$mean, summary(Month_for_if_else)$mean, summary(Month_for_ifelse)$mean, summary(Month_for_switch)$mean, summary(Month_which)$mean, summary(Month_join)$mean, summary(Month_ddply)$mean, summary(Month_str_replace_all)$mean) Season<-c(summary(Season_for_if)$mean, summary(Season_for_if_else)$mean, summary(Season_for_ifelse)$mean, summary(Season_for_switch)$mean, summary(Season_which)$mean, summary(Season_join)$mean, summary(Season_ddply)$mean, summary(Season_str_replace_all)$mean) All<-c(summary(result_for_if)$mean, summary(result_for_if_else)$mean, summary(result_for_ifelse)$mean, summary(result_for_switch)$mean, summary(result_which)$mean, summary(result_join)$mean, summary(result_ddply)$mean, summary(result_str_replace_all)$mean) df<-data.frame(Month/n,Season/n,All/n) colnames(df)<-c("Month","Season","All") df$Type<-c("for_if","for_if_else","for_ifelse","for_switch","which","join","ddply","result_str_replace") df$n<-n df$N<-N return(select(df,Type,n,N,everything())) }
調用上述函數,處理月份數據爲100,200,300,……,1000時,所須要的平均時間git
result<-data.frame(Type=as.character(),n=as.integer(),N=as.integer(), Month=as.integer(),Season=as.integer(),All=as.integer()) foreach (i= seq(100,1000,100)) %dopar% { tmp<-methods_time(i,100) result<-rbind(result,tmp) cat(paste0(i,"\n")) } write.csv(tmp,"/home/xh/300G/tmp/result.csv")
(未完!待續……)函數