Breakpoints cause enormous slowdown

Bruce Dawson comments at cygnus-software.com
Mon Oct 29 01:53:53 EST 2001


I've been playing around with PythonWin and a proprietary debugger with
a common derivation, and they both have a serious problem. I'm worried
that this problem may be fundamental to Python debuggers.

If I run this simple program outside of the debugger, or under PythonWin
without any breakpoints set, it runs very fast - approximately 0.02
seconds (although the timing is quite inaccurate on my system at this
level).

If I set a breakpoint on "import time", run under the debugger to the
breakpoint, then clear the breakpoint and run it, the performance is
identical.

However, if I leave the breakpoint in place then the run time goes to
approximately 9.3 seconds. In other words, the performance drops by
approximately five hundred to one!!! Here's the code:

def DoNothing():
    maximum = 200
    for x in range(maximum):
        for y in range(maximum):
            pass

import time
start = time.time()
DoNothing()
print "Elapsed is %g.", time.time() - start

The code itself is innocuous - you'll get similar results with most
Python code, although I assume that some code will be more dramatic.
Note that adding a few additional breakpoints does not measurably affect
performance.

My understanding is that when there is a breakpoint set that the
debugger has to run in a single step mode and check a list of
breakpoints after each instruction. If that is so then a 500:1 drop in
performance is not surprising.

Other language/debugger pairs frequently solve this problem with a
breakpoint instruction. On the x86 with VisualC++ the instruction used
is "int 3". The equivalent for Python would be a breakpoint byte code.
If this was added to Python then code could run at full speed and could
automatically tell when it got to a breakpoint because the special
breakpoint code would be executed.

Has this been discussed before? Is this possible?

The benefits would be huge. Right now there are some classes of problems
that cannot be debugged with a debugger in Python because of the
tremendous slowdowns that occur.

Thoughts?

Bruce Dawson





More information about the Python-list mailing list