小结(2)
以上用到了9个方法实现一个问题,在实现的过程中试验数据量为n=10。得到不同方法所用的平均耗时间大小。每种方法在计算平均耗时的重复次数为N =100。当然上述的每个方法测试的数据量尽管相同,但由于(1)数据内容不尽相同,(2)由于测试耗时的时候后台打开的程序多少不同(CPU和内存任务量不同),(3)每种方法所处理的内容不尽相同。这些都对所测试的结果产生影响。为此,为了减小这些影响,本节主要通过增加数据量大小(n)(也可以增加重复次数(N ),本例没加以讨论)来估测每种方法的优劣。另外,为了具有可比性,以下统计结果均为处理单个数据所消耗的时间。时间单位为微秒(microsecond)
自定义函数(1)
计算这9个函数处理n个数据分别所用的平均时间(N为重复次数)
#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()))
}
自定义函数(2)
调用上述函数,处理月份数据为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")
(未完!待续……)