時間の関数を選択してテストプログラムを実行するために必要な
Rにおいて、関数は、統計プログラム実行時間は、一般的に使用されたSys.time()
、またはproc.time()
。しかし、この2つの関数は数回にそれは無力に表示され、統計的平均時間を繰り返すことに時間差決意プロセスに従って実行時間後に使用することができます。
ここでは、使用microbenchmark
統計プログラムの実行時間のための機能パッケージを。使用することは非常に簡単です、だけテストするためのコードを入力する必要があり、指定された「時間= N」、プログラムコードを実行します。この機能は、N回繰り返し、その後、平均実行時間を返しています。デフォルトの回= 100の場合。
注:外観からこの研究ショーのみ演算結果は、そのような複雑さを探求するための時間と空間の複雑さの側面として実行されていません。この部分には、次の2件の記事を参照することができます:
- 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:実装されている場合+のための文
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)
(......つづく!であるために)