This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: timeit should start with 1 loop, not 10
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Enhance the timeit module: display average +- std dev instead of minimum
View: 28240
Assigned To: Nosy List: beng94, mark.dickinson, martin.panter, nomeata, serhiy.storchaka
Priority: normal Keywords: patch

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 issue28240.

$ 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:15adminsetgithub: 68203
2016-11-29 19:54:36serhiy.storchakasetstatus: open -> closed
superseder: Enhance the timeit module: display average +- std dev instead of minimum
messages: + msg282037

resolution: duplicate
stage: resolved
2016-02-10 14:39:42beng94setmessages: + msg260014
2016-02-10 13:14:45serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg260010
2016-02-10 11:01:20beng94setfiles: + timeit_num.patch

nosy: + beng94
messages: + msg260004

keywords: + patch
2015-04-20 19:22:47mark.dickinsonsetnosy: + mark.dickinson

messages: + msg241674
versions: + Python 3.4, Python 3.5
2015-04-20 14:28:49martin.pantersetnosy: + martin.panter
2015-04-20 13:28:04nomeatacreate