Are you looking for my non-technical blog?

This is now my technical-only blog, my non-technical blog is here.

08 December 2012

Python Profiler

Was writing a code today and it was so unoptimized and slow. A friend of mine told me about the Profilers. They are a nice way to see which parts of your code are swallowing all of the execution time to work on optimizing them later on.

Here is how to use 'cProfile' in Python. Let's say your main function is called 'main()', so instead of directly calling it, write down the following code:
import cProfile
import pstats
cProfile.run('main()','my_prof')
p = pstats.Stats('my_prof')
p.sort_stats('time').print_stats(10) 
By the way, it came out that 'math.pow(x,2)' was one of the bad guys here, and replacing it with a simple 'x*x' improved the performance a bit.

No comments:

Post a Comment