【发布时间】:2017-02-07 17:04:36
【问题描述】:
问题描述:
我有这个自定义的“校验和”功能:
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 实现中测试相同的确切函数性能的最佳和最准确的方法是什么?
【问题讨论】:
-
那会测试(时间)什么吗?您似乎只执行了一个设置,没有真正的测试命令?
-
@MSeifert 啊,我是个白痴,你是绝对正确的。那里只设置了,我已经更新了答案,留下了问题的后半部分。谢谢!
标签: python performance benchmarking pypy