random.randint() slow, esp in Python 3

Chris Angelico rosuav at gmail.com
Thu Sep 22 19:22:44 EDT 2011

On Fri, Sep 23, 2011 at 4:14 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> What makes you think it's in C? I don't have Python 3.3a, but in 3.2 the
> random module is mostly Python. There is an import of _random, which
> presumably is in C, but it doesn't have a randint method:

True. It seems to be defined in cpython/lib/random.py as a reference
to randrange, which does a pile of error checking and calls
_randbelow... which does a whole lot of work as well as calling
random(). Guess I should have checked the code before asking!

There's probably good reasons for using randint(), but if you just
want a pile of more-or-less random integers, int(random.random()*top)
is the best option.

> I'm not seeing any significant difference in speed between 2.6 and 3.2:
> [steve at sylar ~]$ python2.6 -m timeit -s "from random import
> randint" "randint(0, 1000000)"
> 100000 loops, best of 3: 4.29 usec per loop
> [steve at sylar ~]$ python3.2 -m timeit -s "from random import
> randint" "randint(0, 1000000)"
> 100000 loops, best of 3: 4.98 usec per loop

That might be getting lost in the noise. Try the list comp that I had
above and see if you can see a difference - or anything else that
calls randint that many times.

Performance-testing with a heapsort (and by the way, it's
_ridiculously_ slower implementing it in Python instead of just
calling a.sort(), but we all knew that already!) shows a similar
difference in performance. As far as I know, everything's identical
between the two (I use // division so there's no floating point
getting in the way, for instance), but what takes 90 seconds on Py2
takes 150 seconds on Py3. As with the randint test, I switched int()
to long() to test Py2, and that slowed it down a little, but still far
far below the Py3 time.

I've pasted the code I'm using here: http://pastebin.com/eQPHQhD0

Where's the dramatic performance difference? Or doesn't it matter,
since anything involving this sort of operation needs to be done in C


More information about the Python-list mailing list