Warnings killing my performance

Kylotan kylotan at hotmail.com
Sat Feb 7 07:45:12 EST 2004


"Terry Reedy" <tjreedy at udel.edu> wrote in message news:<mailman.1322.1076118385.12720.python-list at python.org>...
> "Kylotan" <kylotan at hotmail.com> wrote in message
> news:153fa67.0402060933.13840e8c at posting.google.com...
> > And my second is, is there a quick way of taking an integer, shifting
> > it left 13 bits (or multiplying by 2 ** 13, whatever), discarding any
> > excess bits, and which won't cause a warning?
> 
> Have you tried masking off the upper 13 bits *before* the shift?  So that
> there is no overflow to warn about?  Does this make any sense?

Yes, it makes perfect sense... but unfortunately getting rid of that
specific warning isn't enough. As noted in my first post, something is
going through the warnings mechanism silently. I changed the code to
this:

def IntToRandFloat(x):
    """Given a 32-bit integer, return a float in [-1.0, 1.0]"""
    x = int(x) & 0x3FFFF # Preserve lowest 18 bits, to remove
overflow.
    x = int(x << 13) ^ x # Shift left 13 bits.
    return (1.0-((x*(x*x*15731+789221)+1376312589)&0x7fffffff)/1073741824.0)

And the profiler is still showing a call to warn() and warn_explicit()
for each call to IntToRandFloat. I'm also having to preserve 18 bits
instead of the desired 19 because the warning displays when I use 18,
presumably because it includes the sign bit. However now I think I am
damaging the distribution of the function (which is supposed to
generate deterministic noise).

I probably should have mentioned earlier that I'm using "Python 2.3.3
(#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on win32" for
what it's worth.

If I knew what warning it was, maybe I could turn it into an error to
find out what's causing it. But right now, it's a silent warning that
I can't do much about. And which is slowing down my code a lot!

I'm starting to think that I might need to write a 1-function C++
extension here, which would be a shame.

-- 
Ben Sizer



More information about the Python-list mailing list