在分析

时间:2018-04-16 22:57:19

标签: python python-2.7 ipython profiling

我有2个基本上做同样事情的函数,我想比较它们的运行时性能。

def calculate_bonus_by_table(data, value):
    cdf = data["cdf"]

    # CAUTION: the following loop doesn't do bound checking
    i = 0
    while (value > cdf[i]): i += 1

    return data['bonus'][i]

def calculate_bonus_by_regression(data, value):
    max_slope = len(data['bonus']) - 1
    slope = int((value - 1) / 6)
    if slope > max_slope:
        slope = max_slope

    slope += 1
    return (0.205078125 * slope**2) + (0.68359375 * slope) - 70.888671875

data = json.load(open('bonus.json'))    

上面使用的JSON文件的片段

{ "cdf": [6, 12, 18, 24, 30, 36, ...], "bonus": [-70, -68, -66, -64, -62, ...] }

在iPython中,我将该功能单独计时

%timeit calculate_bonus_by_table(data, 199)
1000000 loops, best of 3: 1.64 µs per loop

%timeit calculate_bonus_by_regression(data, 199)
The slowest run took 7.99 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 604 ns per loop

多次运行两个函数的timeit总能给出类似的结果。 .._ by_regression函数结果总是会提示缓存。

如果一个缓存而另一个不缓存,我如何比较2?为什么_by_regression函数被缓存而_by_table不是?在生产环境中使用_by_regression性能和缓存是否成立,或者我应该假设性能最差(从上面开始,7.99 * 604ns = 4.83us)?

由于

0 个答案:

没有答案