On Mon, Jan 11, 2021 at 4:42 AM Alex Prengère <alexpreng...@gmail.com> wrote:
>
> 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?
>

Hmm, might run into problems wherein the script execution time isn't
really repeatable in that sense. You want to NOT measure interpreter
startup time, but you do want to measure the one-off execution time.

Are you able to redefine the important part of the code so it's a
function? If so, you can use timeit in the normal way (import your
script, then time the calls to that function.)

Are you trying to measure the time cost of all your top-level imports,
but not all the time that the interpreter itself consumes? I think
your best bet will be to time the running of the script, then subtract
out the time taken for a null script.

ChrisA
_______________________________________________
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/HGEXAILY7KP2RNXB74CYTS2AK6GV2A2I/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to