Title: Add the --duplicate option for timeit
Type: enhancement Stage: patch review
Components: Demos and Tools, Library (Lib) Versions: Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: alex, georg.brandl, pitrou, r.david.murray, rhettinger, serhiy.storchaka, steven.daprano, tim.peters, vstinner, vstinner
Priority: normal Keywords:

Created on 2017-04-16 06:12 by serhiy.storchaka, last changed 2017-06-07 09:56 by serhiy.storchaka.

Pull Requests
URL Status Linked Edit
PR 1161 open serhiy.storchaka, 2017-04-16 06:15
Messages (4)
msg291736 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-04-16 06:12
One of the most used by me option of the "perf timeit" subcommand is --duplicate. It duplicates statements to reduce the overhead of the loop. This is necessary when measure the time of very fast statements. Proposed patch adds this option for CLI of the timeit module.

Similar feature already was proposed in issue21988, but it automatically duplicated statements if they executed too fast. This patch does this only on explicit request. And it affects only command-line interface. You need to duplicate statements manually when use programming interface.
msg291771 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-04-17 00:47
+1 I've long used this technique when timing fast statements.  See for an example.
msg295317 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-06-07 08:14
About the command line interface, I would expect that "timeit -n 10 --duplicate 10 STMT" would run STMT 100 times. I chose this behaviour in perf timeit:

Current behaviour chosen by Serhiy, -n3 --duplicate=3 runs STMT 3 times:

$ ./python -m timeit --duplicate=3 -r 1 -n 3 'print("bla")'
3 loops, best of 1: 21.8 usec per loop
msg295326 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-06-07 09:56
Ah, I even didn't know that "perf timeit" supports -n since it doesn't report the number of loops. And seems it runs the benchmark much more times, since even with -n1 it is slow. If the number of loops is determined automatically, it would do not matter.

I choose the meaning of --duplicate so that it almost not affect the total time of the benchmarking. Larger value just decreases the overhead of the iteration.
Date User Action Args
2017-06-07 09:56:33serhiy.storchakasetmessages: + msg295326
2017-06-07 08:14:50vstinnersetmessages: + msg295317
2017-05-04 05:49:43serhiy.storchakasetassignee: serhiy.storchaka
2017-04-17 02:47:45gvanrossumsetnosy: - gvanrossum, Guido.van.Rossum
2017-04-17 00:47:21rhettingersetmessages: + msg291771
2017-04-16 12:08:45arigosetnosy: - arigo
2017-04-16 06:15:52serhiy.storchakasetpull_requests: + pull_request1290
2017-04-16 06:12:44serhiy.storchakacreate