2015-10-07 58 views
0

R,我怎麼能清楚地比較不同的解決方案,以相同的問題,他們之間的「公平」? 在別人改變後者的性能之前,能否運行資源消耗型解決方案? 每次測試之間如何「清潔」機器的狀態?在R中用相同的目標清潔比較不同方法的性能?


假設我要計算平均在矩陣的列,我能做到這一點的輕鬆或複雜的方式:

set.seed(9) 
N = 1e7 
ncol = 1e3 
myT = matrix(runif(N), ncol = ncol) 

func1 <- function(mat) { 
    colMeans(mat) 
} 

func2 <- function(mat) { 
    apply(mat, 2, function(x) sum(x)/length(x)) 
} 

func3 <- function(mat) { 
    nrows = c() 
    for (i in 1:nrow(mat)) { 
    nrows = c(nrows, 1) # yes, this is very stupid ;-) 
    } 
    colSums(mat)/sum(nrows) 
} 


system.time(replicate(1, t1 <- func1(myT))) 
# user system elapsed 
# 0.012 0.000 0.011 
system.time(replicate(1, t2 <- func2(myT))) 
# user system elapsed 
# 0.136 0.036 0.170 
system.time(replicate(1, t3 <- func3(myT))) 
# user system elapsed 
# 0.140 0.032 0.170 

運行幾次system.time()執行可以給出不同的結果相同的測試(可能會改變結論)。我注意到特別是更復雜的資源提取解決方案的情況,而最乾淨的解決方案往往具有更一致的執行時間 - 這是什麼原因?如何避免執行同一個表達式之間的巨大變化,以及如何防止它們相互干擾?

是測試之間對gc()的調用有用,是否足夠?

我也瞭解microbenchmark包,但我正在尋找更多'手動'的東西來了解發生了什麼。

我與RStudio工作,如果它的事項......

回答

1

microbenchmark是設計這一點。 system.time()不作詳細介紹

set.seed(9) 
N = 1e5 
ncol = 1e3 
myT = matrix(runif(N), ncol = ncol) 

library(microbenchmark) 
microbenchmark(
    colmeans = colMeans(myT), 
    wrong_apply = apply(myT, 2, function(x) sum(x)/length(x)), # wrong in case of NA 
    correct_apply = apply(myT, 2, mean, na.rm = TRUE), # correct in case of NA 
    stupid = { 
    nrows = c() 
    for (i in 1:nrow(myT)) { 
     nrows = c(nrows, 1) # yes, this is very stupid ;-) 
    } 
    colSums(myT)/sum(nrows) 
    } 
) 

輸出

Unit: microseconds 
      expr  min  lq  mean median  uq  max neval cld 
     colmeans 87.235 92.367 96.44175 95.787 98.781 129.142 100 a 
    wrong_apply 3004.886 3071.595 3483.02090 3166.739 3267.445 18707.947 100 b 
correct_apply 7595.387 7895.148 8850.87886 8106.179 8461.745 13928.438 100 c 
     stupid 144.109 156.510 166.15237 163.351 171.690 255.290 100 a 
+0

由於@Thierry。正如問題中提到的,我意識到「微基準」,並且正在尋找其他東西,但看起來這仍然是最充分的選擇 – ztl

相關問題