[Numpy-discussion] Strange behaviour with for loops + numpy arrays
Francesc Alted
faltet at pytables.org
Mon Jan 17 12:22:17 EST 2011
A Monday 17 January 2011 17:02:43 Stefan Reiterer escrigué:
> Hi all!
>
> I made some "performance" tests with numpy to compare numpy on one
> cpu with mpi on 4 processesors, and something appears quite strange
> to me:
>
> I have the following code:
>
> N = 2**10*4
> K = 16000
>
> x = numpy.random.randn(N).astype(numpy.float32)
> x *= 10**10
> print "x:", x
> t1 = time.time()
>
> #do something...
> for k in xrange(K):
> x *= 0.99
>
> print "altered x:", x
>
> t = time.time() - t1
> print "# loops:", K, "time needed:", t, " s "
>
> # loops: 1000 time needed: 0.0134310722351 s
> # loops: 2000 time needed: 0.028107881546 s
> # loops: 4000 time needed: 0.0367569923401 s
> # loops: 8000 time needed: 0.075756072998 s
> # loops: 16000 time needed: 2.11396384239 s
>
> So for K = 16000 it didn't need twice the amount of time as expected,
> it took 20 x more time! After that jump it seem to "normalize"
> # loops: 32000 time needed: 8.25508499146 s
> # loops: 64000 time needed: 20.5365290642 s
>
> First I suspected xrange was the culprit, but if I tried
> k = 0
> while k < K:
> x *= 0.99
>
> it changed anything.
> When I tried simply
> a=0
> for k in xrange(K):
> a = a+1
>
> none of the effects above triggered, so I suspect that numpy has to
> be involved. My Hardware is 2.3 GHz Intel Dual Core, 2 GB Ram and
> Ubuntu 10.04. For my tests I tried it with Python 2.6, and Sage 4.6.
> (which uses 2.6 too)
>
> Also changing the size of arrays or changing the computer didn't
> help.
>
> Has anyone an Idea what had could happen?
You are generating denormalized numbers:
http://en.wikipedia.org/wiki/Denormal_number
Many processors cannot deal efficiently with these beasts in hardware.
You may want to convert these numbers to zero if you want more speed.
--
Francesc Alted
More information about the NumPy-Discussion
mailing list