首页 > 解决方案 > 根据内存使用量量化性能

问题描述

如何量化不同功能(或方法)的性能?我正在考虑类似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())

标签: r

解决方案


推荐阅读