views:

677

answers:

3

This questions is semi-based of this one here:

http://stackoverflow.com/questions/582336/how-can-you-profile-a-python-script

I thought that this would be a great idea to run on some of my programs. Although profiling from a batch file as explained in the aforementioned answer is possible, I think it would be even better to have this option in Eclipse. At the same time, making my entire program a function and profiling it would mean I have to alter the source code?

How can I configure eclipse such that I have the ability to run the profile command on my existing programs?

Any tips or suggestions are welcomed!

A: 

You can always make separate modules that do just profiling specific stuff in your other modules. You can organize modules like these in a separate package. That way you don't change your existing code.

Vasil
+2  A: 

if you follow the common python idiom to make all your code, even the "existing programs", importable as modules, you could do exactly what you describe, without any additional hassle.

here is the specific idiom I am talking about, which turns your program's flow "upside-down" since the __name__ == '__main__' will be placed at the bottom of the file, once all your defs are done:

# program.py file

def foo():
    """ analogous to a main().  do something here """
    pass

# ... fill in rest of function def's here ...

# here is where the code execution and control flow will
# actually originate for your code, when program.py is
# invoked as a program.  a very common Pythonism...
if __name__ == '__main__':
    foo()

In my experience, it is quite easy to retrofit any existing scripts you have to follow this form, probably a couple minutes at most.

Since there are other benefits to having you program also a module, you'll find most python scripts out there actually do it this way. One benefit of doing it this way: anything python you write is potentially useable in module form, including cProfile-ing of your foo().

popcnt
A: 

Is there any update from PyDev side from this Eclipse integrated Python profiling question?

Gökhan Sever