• Increase font size
  • Default font size
  • Decrease font size
joomla templates, wordpress themes, drupal, datalife engine, graphics, seo,

Using a Profiler and RunSnakeRun to Troubleshoot or Optimize Python Code

E-mail Print PDF

I had a piece of code today that was running really slow. I just couldn't figure out why. Whenever I thought I had the answer, it turned out I was wrong. So I decided to run a profiler. Profiling your code lets you know where all the time is spent. You can see how long on average a specific function call takes and how many times a specific function was called.

To use the profiler included with Python, import profiler or cProfiler. cProfiler is C based where profiler is all python. In general, you should use cProfiler if it's available and works for you since it's much faster. Once the module is imported simply launch the code you want to profile through the run command:'foo()')'foo()', 'c:/TEMP/profile.txt')

The first example runs the profiler and outputs the results to the console when it's done. The second example saves out a profile file which can then be used with a program to display or analyze the data. I like to use RunSnakeRun with a saved profile file to get a graphical representation of what was happening.

Using RunSnakeRun is pretty simple. Just open your profile and you'll see multiple views of the information. There is a column view showing all the function calls. This shows how many times each function was called, the average runtime of the function per call, the total amount of time spent in each function, and a few other fields. There is also the graphical view. This view starts by showing you the entire program. It is broken up graphically into how much time was spent in each function called by the main loop. The bigger the square, the more time was spent. Each of these squares has the same information within it. By selecting any function in the list, or square in the interface you can make that the top level in the graphical view.

Generally one of three things helps me identify where my bottleneck is:

  1. A function that's called many more times than expected.

    If you expect paint() to be called 3 or 4 times during your test, but it's called 10,375 times, you may have a problem.

  2. The total time spent in a function is higher than expected.

    If your search() function takes 121 seconds, and 114 of those seconds are spent calling paint 10,375 times, you might have a problem.

  3. The individual execution of a function takes much longer than expected.

    If you expect your replace() function to take a few seconds but it is stuck on your search() function taking a few minutes, you could have a problem.

As you can see, an issue in your code probably leaves a trail. Just work your way through and find the source.



0 #1 odoo installation 2015-04-09 14:32
For starters, it eliminated the need for manual activity, since everything
today is practically done using computers. Make sure that you challenge the timeline given to you by your salesperson and the resources required on your
end to make a successful implementation happen. Streamlines customer invoicing so that there are no leakages in the billing.

Add comment

Security code

Main Menu