On Fri, 13 Nov 2009 15:33:53 +1300, greg wrote:

> Santiago Romero wrote:
>>>How about
>>>    page, index = divmod(address, 16384)
>>
>>  Surely, much better and faster :-)
> 
> Not necessarily, because it involves a function call, and constructing
> and deconstructing a result tuple. If you time them, you may well find
> that the explicit shift and mask operations turn out to be faster.

It's easy enough to test:

>>> from timeit import Timer
>>> t1 = Timer('a = n>>14; b = n & 16384', 'n=2137902')
>>> t2 = Timer('a,b = divmod(n, 16384)', 'n=2137902')
>>> min(t1.repeat(repeat=5))
0.32850909233093262
>>> min(t2.repeat(repeat=5))
0.54839301109313965

The shift and mask are a little faster on my machine, but that's 
certainly what I would call a micro-optimization. Unless the divmod call 
is the bottleneck in your code -- and it almost certainly won't be -- I 
don't think it's worth the obfuscation to use shift/mask. What should be 
done is write the code in the clearest way you can, then, only if it is 
it too slow, profile it to see where it actually needs optimizing.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to