简体   繁体   English

为什么python timeit结果与shell的time命令相矛盾?

[英]Why python timeit result is in contradiction with shell's time command?

I'm Not quite sure what I'm missing here, but looks like time from shell reports higher values for commands that timeit from python report less values for. 我不太清楚,我在这里失踪,但看起来像time从该命令外壳报告值越高timeit从蟒蛇报告少值。 And practically looks like the time numbers are correct. 实际上看起来time数字是正确的。 What is happening? 怎么了?

time python -m timeit 'd={"i": 1}; d.update({"i": 2, "j": 3, "k": 4})'

leads to: 导致:

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})'

leads to: 导致:

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'

leads to: 导致:

10000000 loops, best of 3: 0.145 usec per loop (timeit)
5.98s user 0.00s system 99% cpu 5.986 total (time)                                                                                                                                                                                    
  • time tells you how long it takes to run the benchmark . time告诉你运行基准测试需要多长时间
  • timeit tells you how long it takes to run the code the benchmark is built to measure . timeit告诉您运行基准测量所需的代码需要多长时间

These are two very different things. 这是两件截然不同的事情。 time includes all the overhead of starting the interpreter; time包括启动翻译的所有开销; of collecting the statistics on how long your individual benchmarked runs take; 收集有关您的个人基准测试运行时间的统计数据; of interpreting those results; 解释这些结果; of doing any additional runs which got thrown out because they were unstable or otherwise statistically unsuitable; 做任何额外的跑步,因为它们不稳定或在统计上不合适而被抛出; of performing garbage collection between timed invocations; 在定时调用之间执行垃圾收集; etc. 等等

看起来结果并不矛盾,因为第三种情况在10,000,000次运行10次以上,而情况1和2运行1,000,000次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM