處理時間如何隨資料大小改變?
如果你要處理兩個資料集的所有元素,而其中一個更大,通常較大的那個會花比較久的時間。不過,要注意的是,所需時間增加的幅度不一定會和資料集變大的比例成正比。也就是說,如果兩個資料集中有一個是另一個的 2 倍大,並不保證較大的那個就會剛好多花 2 倍的時間。它可能只多花 1.5 倍,甚至多到 4 倍。這取決於你用哪些操作來處理資料集。
在這個練習中,你會使用 microbenchmark 套件,這在 Writing Efficient R Code 課程 中有介紹。
注意:數字以科學記號表示 $$1e5 = 1 * 10^5 = 100,000$$。
本練習屬於課程
R 的可擴展資料處理
練習說明
- 載入
microbenchmark套件。 - 使用
microbenchmark()函式來比較隨機向量的排序時間。 - 對
mb呼叫plot()。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Load the microbenchmark package
___
# Compare the timings for sorting different sizes of vector
mb <- ___(
# Sort a random normal vector length 1e5
"1e5" = sort(rnorm(1e5)),
# Sort a random normal vector length 2.5e5
"2.5e5" = sort(rnorm(2.5e5)),
# Sort a random normal vector length 5e5
"5e5" = sort(rnorm(5e5)),
"7.5e5" = sort(rnorm(7.5e5)),
"1e6" = sort(rnorm(1e6)),
times = 10
)
# Plot the resulting benchmark object
___(mb)