How can I make this piece of code even faster?

Stefan Behnel stefan_ml at behnel.de
Sun Jul 21 08:49:44 EDT 2013


pablobarhamalzas at gmail.com, 21.07.2013 12:48:
> El domingo, 21 de julio de 2013 12:31:42 UTC+2, Steven D'Aprano  escribió:
>> [steve at ando ~]$ python3.3 -m timeit -s "x = 2.357e7" "x**0.5"
>> 1000000 loops, best of 3: 0.319 usec per loop
>> [steve at ando ~]$ python3.3 -m timeit -s "x = 2.357e7" -s "from math import 
>> sqrt" "sqrt(x)"
>> 10000000 loops, best of 3: 0.172 usec per loop
>>
>> How exactly are you timing the code?
> 
> I'm timing the whole program with cProfile. Removing math.sqrt() from a function and using **(1/2) instead cut the execution time for a significant amount (~0.035 to ~0.020).

With or without the profiler running? Note that profiling will slow down
your code (especially function calls), often significantly and sometimes
even in such an unbalanced way that it visibly changes its execution
profile. Always make sure you validate your code changes with benchmarks,
outside of the profiler.

Stefan





More information about the Python-list mailing list