小結(jié)(2)
以上用到了9個方法實現(xiàn)一個問題碧绞,在實現(xiàn)的過程中試驗數(shù)據(jù)量為n=10玉吁。得到不同方法所用的平均耗時間大小。每種方法在計算平均耗時的重復(fù)次數(shù)為N =100排霉。當(dāng)然上述的每個方法測試的數(shù)據(jù)量盡管相同偏竟,但由于(1)數(shù)據(jù)內(nèi)容不盡相同煮落,(2)由于測試耗時的時候后臺打開的程序多少不同(CPU和內(nèi)存任務(wù)量不同),(3)每種方法所處理的內(nèi)容不盡相同踊谋。這些都對所測試的結(jié)果產(chǎn)生影響蝉仇。為此,為了減小這些影響殖蚕,本節(jié)主要通過增加數(shù)據(jù)量大薪蜗巍(n)(也可以增加重復(fù)次數(shù)(N ),本例沒加以討論)來估測每種方法的優(yōu)劣睦疫。另外害驹,為了具有可比性,以下統(tǒng)計結(jié)果均為處理單個數(shù)據(jù)所消耗的時間蛤育。時間單位為微秒(microsecond)
自定義函數(shù)(1)
計算這9個函數(shù)處理n個數(shù)據(jù)分別所用的平均時間(N為重復(fù)次數(shù))
#n為隨機化月份數(shù)據(jù)向量的長度宛官,N為計算每個函數(shù)平均重復(fù)的次數(shù)
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()))
}
自定義函數(shù)(2)
調(diào)用上述函數(shù),處理月份數(shù)據(jù)為100瓦糕,200底洗,300,……刻坊,1000時枷恕,所需要的平均時間
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")
(未完党晋!待續(xù)……)