问题描述:
我有这个自定义的"checksum“函数:
NORMALIZER = 0x10000
def get_checksum(part1, part2, salt="trailing"):
"""Returns a checksum of two strings."""
combined_string = part1 + part2 + " " + salt if part2 != "***" else part1
ords = [ord(x) for x in combined_string]
checksum = ords[0] # initial value
# TODO: document the logic behind the checksum calculations
iterator = zip(ords[1:], ords)
checksum += sum(x + 2 * y if counter % 2 else x * y
for counter, (x, y) in enumerate(iterator))
checksum %= NORMALIZER
return checksum
我想在Python3.6和PyPy上测试它的性能。我想看看这个函数在PyPy上是否会表现得更好,但我不能完全确定,什么是最可靠、最干净的方法。
我已经尝试过的东西和问题:
目前,我对这两种情况都使用timeit
:
$ python3.6 -mtimeit -s "from test import get_checksum" "get_checksum('test1' * 100000, 'test2' * 100000)"
10 loops, best of 3: 329 msec per loop
$ pypy -mtimeit -s "from test import get_checksum" "get_checksum('test1' * 100000, 'test2' * 100000)"
10 loops, best of 3: 104 msec per loop
我担心的是,由于potential JIT warmup overhead的存在,我不能完全确定timeit
是否是在PyPy
上工作的合适工具。
此外,在报告测试结果之前,PyPy本身会报告以下内容:
WARNING: timeit is a very unreliable tool. use perf or something else for real measurements
pypy -m pip install perf
pypy -m perf timeit -s 'from test import get_checksum' "get_checksum('test1' * 1000000, 'test2' * 1000000)"
要在这些Python实现和其他Python实现之间测试相同的函数性能,最好、最准确的方法是什么?
发布于 2017-03-30 21:06:53
您可以使用--repeat
参数增加重复次数,以提高计时精度。请参见:
发布于 2017-04-06 18:56:39
目前还不完全清楚你想要测量的是什么。根据你的用例,“性能”可以有多种含义。
--repeat
。有了足够的重复,在代码的其他部分花费的时间将逐渐变得"insignificant".如果你正在尝试提高速度,使用像cProfile这样的分析器可以帮助隔离你想要测量其速度的代码?
我实际上不是在回答你的问题,但我希望它能有所帮助:)
https://stackoverflow.com/questions/42095793
复制相似问题