timeit module for comparing the performance of two scripts

Fredrik Lundh fredrik at pythonware.com
Tue Jul 11 12:06:56 EDT 2006


"Phoe6"

>> $ python -m timeit -s "import pyConfig" "pyConfig.pyConfig()"
>> $ python -m timeit -s "import pyConparse" "pyConparse.pyConParse()"
>
>> note that timeit runs the benchmarked function multiple times, so you may want
>> to remove the print statements.
>
> Thanks a lot Fredrik!. I did not know that timeit runs benchmarked
> function multiple times. I got scared with multiple prints and thought
> import pyConfig has put it in infinite loop and I had killed the
> program.
>
> I could use Timer function as well.

for cases like this, the command form gives a better result with less effort; it picks
a suitable number of iterations based on how fast the code actually runs, instead of
using a fixed number, and it also runs the test multiple times, and picks the smallest
observed time.

</F> 






More information about the Python-list mailing list