Hello,
Today I had a quite simple need, I am unsure about the best way to do it,
and saw a possible improvement for the *timeit *module.

I have about 30 Python scripts and I want to measure precisely their
execution times, without measuring the interpreter startup time, because
for most of them it is quite short (<1ms).

I see several ways to do this:
0. Just measure the execution time with "time python script.py"
    > Not great for me, because for the fastest scripts, it is very
imprecise.

1. Modify the scripts to add some "import time; start =
time.perf_counter()" at the beginning of each script and
"print(time.perf_counter() - start)" at the end of each script.
    >  I would prefer to not change the scripts source code, but I agree
that this works otherwise.

2. Have a way to dynamically inject those "time.perf_counter()" as an
command line option. I searched and do not think this is currently
possible, but 2 options looking like that would work :
$ python script.py --before 'import time; start = time.perf_counter()'
--after 'print(time.perf_counter() - start)'

3. Use timeit. The scripts have no side effects so repeating their
execution the way timeit does, works for me. The only issue is that, as far
as I know, timeit only allows statements as input parameters, not the whole
script, like for example:
$ python -m timeit --script script.py

4. Write custom code to import each script.

Unless I am mistaken, I feel like solution #3 with a new option of timeit
is the most appropriate. I may have missed something more obvious though.

What do you think?
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/OAIGJAWUUXUEH6SZVCE3Z4PTX63V7GJC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to