python vs perl performance test

Arnaud Delobelle arnodel at googlemail.com
Thu Dec 13 14:38:42 EST 2007


On Dec 13, 6:11 pm, igor.tatari... at gmail.com wrote:

> from random import randrange
> from itertools import imap, repeat
> from operator import getitem, add, getslice
>
> result = 0
> zeros = [0]*100
> for i in xrange (100000):
>     s = [chr(randrange(128))] * 1024
>     starts = repeat(randrange(900), 100)
>     ends = imap(add, starts, repeat(randrange(1,100), 100))
>     substrs = imap(getslice, repeat(s, 100), starts, ends)
>     result += sum(imap(ord, imap(getitem, substrs, zeros)))
>
> print "Sum is ", result
>
> There's got to be a simpler and more efficient way to do this.
> Can you help?
>
> Thanks,
> igor

repeat(randrange(n), p) doesn't do what you want it to:
It generates a single random number less thant n and repeats
it p times

>>> list(repeat(randrange(100), 10))
[54, 54, 54, 54, 54, 54, 54, 54, 54, 54]

Instead you want imap(randrange, repeat(n, p)):

>>> map(randrange, repeat(100, 10))
[69, 68, 81, 26, 60, 76, 40, 55, 76, 75]

I don't understand why you make the Python version so complicated.

--
Arnaud




More information about the Python-list mailing list