Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > subtle error slows code by 10x (builtin sum()) - replace builtin sumwithout using import?

Reply
Thread Tools

subtle error slows code by 10x (builtin sum()) - replace builtin sumwithout using import?

 
 
bdb112
Guest
Posts: n/a
 
      07-02-2011
First a trap for new players, then a question to developers

Code accelerated by numpy can be slowed down by a large factor is you
neglect to import numpy.sum .

from timeit import Timer
frag = 'x=sum(linspace(0,1,1000))'
Timer(frag ,setup='from numpy import linspace').timeit(1000)
# 0.6 sec
Timer(frag, setup='from numpy import sum, linspace').timeit(1000) #
difference is I import numpy.sum
# 0.04 sec 15x faster!

This is obvious of course - but it is very easy to forget to import
numpy.sum and pay the price in execution.

Question:
Can I replace the builtin sum function globally for test purposes so
that my large set of codes uses the replacement?

The replacement would simply issue warnings.warn() if it detected an
ndarray argument, then call the original sum
I could then find the offending code and use the appropriate import to
get numpy.sum
 
Reply With Quote
 
 
 
 
Albert Hopkins
Guest
Posts: n/a
 
      07-02-2011


On Friday, July 1 at 19:17 (-0700), bdb112 said:

> Question:
> Can I replace the builtin sum function globally for test purposes so
> that my large set of codes uses the replacement?
>
> The replacement would simply issue warnings.warn() if it detected an
> ndarray argument, then call the original sum
> I could then find the offending code and use the appropriate import to
> get numpy.sum


You shouldn't do this, but you could use the __builtins__ module

e.g.

>>> __builtins__.sum = numpy.sum # bad




 
Reply With Quote
 
 
 
 
Chris Torek
Guest
Posts: n/a
 
      07-02-2011
In article <(E-Mail Removed)>
bdb112 <(E-Mail Removed)> wrote:
>First a trap for new players, then a question to developers
>
>Code accelerated by numpy can be slowed down by a large factor is you
>neglect to import numpy.sum .
>
>from timeit import Timer
>frag = 'x=sum(linspace(0,1,1000))'
>Timer(frag ,setup='from numpy import linspace').timeit(1000)
># 0.6 sec
>Timer(frag, setup='from numpy import sum, linspace').timeit(1000) #
>difference is I import numpy.sum
># 0.04 sec 15x faster!
>
>This is obvious of course - but it is very easy to forget to import
>numpy.sum and pay the price in execution.
>
>Question:
>Can I replace the builtin sum function globally for test purposes so
>that my large set of codes uses the replacement?
>The replacement would simply issue warnings.warn() if it detected an
>ndarray argument, then call the original sum
>I could then find the offending code and use the appropriate import to
>get numpy.sum



Sure, just execute code along these lines before running any of
the tests:

import __builtin__
import warnings

_sys_sum = sum # grab it before we change __builtin__.sum

def hacked_sum(sequence, start=0):
if isinstance(sequence, whatever):
warnings.warn('your warning here')
return _sys_sum(sequence, start)

__builtin__.sum = hacked_sum

(You might want to grab a stack trace too, using the traceback
module.) You said "without using import" but all you have to
do is arrange for python to import this module before running
any of your own code, e.g., with $PYTHONHOME and a modified
site file.
--
In-Real-Life: Chris Torek, Wind River Systems
Intel require I note that my opinions are not those of WRS or Intel
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this a bug in PERL or a subtle error by me? John W. Kennedy Perl Misc 9 05-05-2004 09:37 PM
10X zoom digicams - need stabilization? Matthew Fleming Digital Photography 22 01-14-2004 11:48 AM
Best 10X zoom camera Harlen Ng Digital Photography 21 12-08-2003 03:24 PM
True 10X optical zoom? Don Digital Photography 30 11-26-2003 06:17 AM
Canon 10x John Worthington Digital Photography 2 11-08-2003 10:44 AM



Advertisments