Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8653 > unrolled thread

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

Started bybdb112 <boyd.blackwell@gmail.com>
First post2011-07-01 19:17 -0700
Last post2011-07-02 03:13 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  subtle error slows code by 10x (builtin sum()) - replace builtin sum without using import? bdb112 <boyd.blackwell@gmail.com> - 2011-07-01 19:17 -0700
    Re: subtle error slows code by 10x (builtin sum()) - replace builtin sum without using import? Albert Hopkins <marduk@letterboxes.org> - 2011-07-01 22:54 -0400
    Re: subtle error slows code by 10x (builtin sum()) - replace builtin sum without using import? Chris Torek <nospam@torek.net> - 2011-07-02 03:13 +0000

#8653 — subtle error slows code by 10x (builtin sum()) - replace builtin sum without using import?

Frombdb112 <boyd.blackwell@gmail.com>
Date2011-07-01 19:17 -0700
Subjectsubtle error slows code by 10x (builtin sum()) - replace builtin sum without using import?
Message-ID<f6dbf631-73a9-485f-8ada-bc7376ac686b@h25g2000prf.googlegroups.com>
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

[toc] | [next] | [standalone]


#8654

FromAlbert Hopkins <marduk@letterboxes.org>
Date2011-07-01 22:54 -0400
Message-ID<mailman.552.1309575260.1164.python-list@python.org>
In reply to#8653

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


[toc] | [prev] | [next] | [standalone]


#8657

FromChris Torek <nospam@torek.net>
Date2011-07-02 03:13 +0000
Message-ID<ium2c102ppo@news4.newsguy.com>
In reply to#8653
In article <f6dbf631-73a9-485f-8ada-bc7376ac686b@h25g2000prf.googlegroups.com>
bdb112  <boyd.blackwell@gmail.com> 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 (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)      http://web.torek.net/torek/index.html

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web