【发布时间】:2019-08-08 21:10:21
【问题描述】:
我不太确定我在这里缺少什么,但看起来 shell 中的 time 报告的命令值较高,而 python 中的 timeit 报告的值较低。实际上看起来time 数字是正确的。发生了什么?
time python -m timeit 'd={"i": 1}; d.update({"i": 2, "j": 3, "k": 4})'
导致:
1000000 loops, best of 3: 0.373 usec per loop (timeit)
1.54s user 0.00s system 99% cpu 1.545 total (time)
time python -m timeit 'd={"i": 1}; d = dict(d, **{"i": 2, "j": 3, "k": 4})'
导致:
1000000 loops, best of 3: 0.43 usec per loop (timeit)
1.77s user 0.00s system 99% cpu 1.778 total (time)
time python -m timeit 'd={"i": 1}; d["i"] = 2; d["j"] = 3; d["k"] = 4'
导致:
10000000 loops, best of 3: 0.145 usec per loop (timeit)
5.98s user 0.00s system 99% cpu 5.986 total (time)
【问题讨论】:
-
糟糕!我错过了第三个实际上跑了 10 倍以上......