Orders of magnitude

Simon Wittber drconrad at metaplay.com.au
Mon Mar 29 21:04:15 EST 2004


>A list 10 million integers suck up ~160 megs of memory with Python.

Sorting a list of 10000000 integers sounds like a job for Numeric.

The below code does the job in 22 seconds. Numeric consumes ~40MB for
the initial array, then grabs another ~40MB, once the sorting starts.
Its about as lean as you can get it.

-------cut-here--------------------------
import Numeric
import RandomArray
import time
print "creating array...",
r = RandomArray.binomial((4294967294/2), .5, 10000000)
print "done."
s = time.clock()
print "sorting array...",
Numeric.sort(r)
print "done."
print "finding duplicates..."
ou = r[0]
dc = 0
for u in r[1:]:
    if u == ou: dc += 1
    else: ou = u
print "done. (%d) duplicates." % dc
print "completed in ", time.clock() - s,  "seconds."
-------cut-here--------------------------

Output:
creating array... done.
sorting array... done.
finding duplicates...
done. (278) duplicates.
completed in  22.2402900369 seconds





More information about the Python-list mailing list