代码之家  ›  专栏  ›  技术社区  ›  d.b

根据内存使用情况量化性能

r
  •  0
  • d.b  · 技术社区  · 7 年前

    如何量化不同功能(或方法)的性能?我在想 system.time() microbenchmark::microbenchmark() 但是对于内存使用。在这个例子中,假设我想比较 foo1() vs foo2() .我试着修补 gc() 但无法将其用于比较。

    set.seed(42)
    x = sample(1:100000)
    y = sample(1:100000)
    
    foo1 = function(){
        S = x + y
        return(tail(which(S == max(S)), 1))
    }
    
    foo2 = function(){
        S = x[1] + y[1]
        i = 1
        for (ind in 2:length(x)){
            if ((x[ind] + y[ind]) > S){
                S = x[ind] + y[ind]
                i = ind
            }
        }
        return(i)
    }
    
    identical(foo1(), foo2())
    
    0 回复  |  直到 7 年前