Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I noticed several times that a simple python script (with some straightforward algebraic computation in a loop) runs considerably faster (up to a factor 6) when launched on a shell command line as compared to a run via IDLE's shell. There is nothing fancy going on in the script. I only print a loop variable to visually follow progress in the loop.

Surely just this print statement cannot be the reason for the speed loss in IDLE, or can it ?

Can someone explain me why this is ?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Yes, the print statement really is the main cause of slow-down within idle. The stdout and stderr streams are captured by IDLE, and a whole bunch of Tk commands are executed to render the text appropriately. Removing your output to stdout should speed things up again.

See the idlelib.PyShell, idlelib.OutputWindow, and idlelib.EditorWindow modules for (some of) the extent of the rabbit hole...

I would suggest using the logging module to record progress; you can then set the logging level to something higher when running in IDLE to avoid output in that condition, or log somewhere other than stdout.

Although I really wouldn't recommend it, something like the following probably works most of the time if you really want to be able to configure behaviour differently when running under IDLE. Though you really shouldn't :-)

import sys

running_under_idle = 'idlelib' in sys.modules
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.