Overflow exceptions are slow!

Tim Peters tim.one at comcast.net
Sun Dec 8 14:32:08 EST 2002


[Aahz]
> In Python 2.2, compare the following two loops for speed:
>
> print "Starting loop 1"
> start = time.clock()
> L1 = []
> for i in loop:
>     L1.append(1L * i * i)
> end = time.clock()
> print "Loop 1 took", end - start, "seconds"
>
> print "Starting loop 2"
> start = time.clock()
> L2 = []
> for i in loop:
>     L2.append(i * i)
> end = time.clock()
> print "Loop 2 took", end - start, "seconds"

It's not really the overflow exceptions that are slow, it's that, during the
transitional period, each overflow takes a trip through the Python warnings
module to determine whether you've enabled the overflow warning.  So each
overflowing instance of "i * i" there invokes a bunch of Python code under
the covers.  OverflowWarning is ignored by default, so you're usually not
aware of this.  It can be enabled, though:

>>> import warnings
>>> warnings.filterwarnings("default", "", OverflowWarning)
>>> 32234324**2
__main__:1: OverflowWarning: integer exponentiation
1039051643736976L
>>>

PEP 237 talks more about this, and OverflowWarning will eventually go away.





More information about the Python-list mailing list