Issue 24015: timeit should start with 1 loop, not 10 (original) (raw)

Created on 2015-04-20 13:28 by nomeata, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
timeit_num.patch beng94,2016-02-10 11:01 review
Messages (6)
msg241643 - (view) Author: Joachim Breitner (nomeata) Date: 2015-04-20 13:28
The docs for the timeit command line interface specify If -n is not given, a suitable number of loops is calculated by trying successive powers of 10 until the total time is at least 0.2 seconds. This sounds as if it it first tries 1, then 10, then 100 etc. But the code starts with 10 iterations. So even if the tested code already takes long enough (e.g. because it is a suitable loop itself), timit will by default test 10 loops. I propose to change that, and replace # determine number so that 0.2 <= total time < 2.0 for i in range(1, 10): number = 10**i with # determine number so that 0.2 <= total time < 2.0 for i in range(0, 10): number = 10**i in Lib/timeit.py.
msg241674 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2015-04-20 19:22
Huh. I assumed that timeit was doing this already. +1 from me. Affects Python 3.4 and 3.5, too. taniyama:~ mdickinson$ time python -m timeit "import time; time.sleep(1.0)" 10 loops, best of 3: 1 sec per loop real 0m40.165s user 0m0.040s sys 0m0.024s
msg260004 - (view) Author: Tamás Bence Gedai (beng94) * Date: 2016-02-10 11:01
I don't know what happened to this issue, but it looks good to me. So here is a patch for it as Joachim suggested. $ time ./python -m timeit "import time; time.sleep(1.0)" 1 loops, best of 3: 1 sec per loop real 0m4.134s user 0m0.116s sys 0m0.004s
msg260010 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-02-10 13:14
I think that only one loop is not enough to get reliable result. The first loop often has an overhead.
msg260014 - (view) Author: Tamás Bence Gedai (beng94) * Date: 2016-02-10 14:39
Then maybe the docs should be clarified. "If -n is not given, a suitable number of loops is calculated by trying successive powers of 10 (starting from 10) until the total time is at least 0.2 seconds."
msg282037 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-11-29 19:54
That was implemented in . $ time ./python -m timeit "import time; time.sleep(1.0)" 1 loop, best of 5: 1 sec per loop real 0m6.176s user 0m0.160s sys 0m0.004s
History
Date User Action Args
2022-04-11 14:58:15 admin set github: 68203
2016-11-29 19:54:36 serhiy.storchaka set status: open -> closedsuperseder: Enhance the timeit module: display average +- std dev instead of minimummessages: + resolution: duplicatestage: resolved
2016-02-10 14:39:42 beng94 set messages: +
2016-02-10 13:14:45 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2016-02-10 11:01:20 beng94 set files: + timeit_num.patchnosy: + beng94messages: + keywords: + patch
2015-04-20 19:22:47 mark.dickinson set nosy: + mark.dickinsonmessages: + versions: + Python 3.4, Python 3.5
2015-04-20 14:28:49 martin.panter set nosy: + martin.panter
2015-04-20 13:28:04 nomeata create