Best search algorithm to find condition within a range

Chris Angelico rosuav at gmail.com
Tue Apr 7 15:25:35 EDT 2015


On Wed, Apr 8, 2015 at 5:19 AM, Ian Kelly <ian.g.kelly at 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 at sikorsky:~$ python3 -m timeit -s "n = 1000000; x = 42" "n % x; n // x"
1000000 loops, best of 3: 0.231 usec per loop
rosuav at sikorsky:~$ python3 -m timeit -s "n = 1000000; x = 42" "divmod(n,x)"
1000000 loops, best of 3: 0.305 usec per loop
rosuav at 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



More information about the Python-list mailing list