On Wed, Apr 8, 2015 at 5:19 AM, Ian Kelly <ian.g.ke...@gmail.com> wrote: >> % and probably // call divmod internally and toss one of the results. >> Slightly faster (5.7 versus 6.1 microseconds on my machine) is > > Not on my box. > > $ python3 -m timeit -s "n = 1000000; x = 42" "n % x; n // x" > 10000000 loops, best of 3: 0.105 usec per loop > $ python3 -m timeit -s "n = 1000000; x = 42" "divmod(n,x)" > 10000000 loops, best of 3: 0.124 usec per loop
Nor mine, though eliminating the global lookup cuts some of the time: rosuav@sikorsky:~$ python3 -m timeit -s "n = 1000000; x = 42" "n % x; n // x" 1000000 loops, best of 3: 0.231 usec per loop rosuav@sikorsky:~$ python3 -m timeit -s "n = 1000000; x = 42" "divmod(n,x)" 1000000 loops, best of 3: 0.305 usec per loop rosuav@sikorsky:~$ python3 -m timeit -s "n = 1000000; x = 42; dm = divmod" "dm(n,x)" 1000000 loops, best of 3: 0.26 usec per loop But this is microbenchmarking. It's pretty pointless. ChrisA -- https://mail.python.org/mailman/listinfo/python-list