在r語言中,統計乙個程式體執行時間一般採用的函式為sys.time()
或者為proc.time()
。不過,這兩個函式只能根據時間差判斷程式執行一次所用的時間,若要重複多次進行平均時間的統計,則顯得無能為力。
在此,我們採用microbenchmark
函式包來進行統計程式執行時間。該函式使用很簡單,只需要輸入待測試**,並且指定「times=n」,程式就會重複執行**n次,然後返回執行時間的平均值。預設的話times=100。
#generating n integer data between 1 to 12
month_digital<
-function
(n)return
(month_digital)
}
任務:根據生成的測試資料(1~12)生成對應的月份的英文名字,並判斷該月份所屬的季節1: 自定義函式
# digital was translated into month's englishname
month_name_for_if<
-function
(month)
return
(month_name)
}# digital was translated into season's englishname
season_name_for_if<
-function
(month)
return
(season_name)
}#generating month and season english
result_for_if<
-function
(n)
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
100unit: 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
100unit: 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
100there were 50 or more warnings (use warnings
() to see the first 50
)
(未完!待續……) 1 R語言介紹
注釋 賦值 檢視 設定當前工作目錄 getwd setwd wd workspace directory setwd c myproject project1 在獨立的目錄中儲存專案是個好主意。文字輸出 函式 skin filename 將輸出重定向到檔案 中。不加引數呼叫 skin 僅向圖形輸出。...
1R安裝環境
2進入 的cocoa gui資料夾中,r框架將安裝在 library frameworks中,tcl tk和texinfo將安裝在 usr local 在某些條件下你可以將其自由散布。用 license 或 licence 來看散布的詳細條件。r是個合作計畫,有許多人為之做出了貢獻.用 contri...
R語言筆記1
生成乙個長度為n的,元素全為0的向量 zero.vector vector mode numeric length n 判斷向量中是否有某元素 vec c 1,3,2,6 3 in vec 1 true 4 in vec 1 false 統計某個向量中所有元素的出現次數 table函式 test c...