searching a list of lists as a two-dimensional array?

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Mon Feb 12 02:55:33 EST 2007


On Sun, 11 Feb 2007 23:20:11 -0800, John Machin wrote:

> Now for the algorithm: all of that testing to see if you are about to
> sail off the end of the world is a bit ugly and slow. You can use bit-
> bashing, as Paul suggested, even though it's on Steven D'Aprano's list
> of 6 deadly sins :-)

Heh. Being a smart-alec is number 7 :-P

Seriously, this is Python. Are you *sure* bit-bashing is going to be
faster than the alternative? If this was C, or assembly, you'd almost
certainly be right. But Python is heavily object-oriented, and bit
manipulations are just methods, with all the overhead that entails.

>>> import timeit
>>> timeit.Timer("3 | 2", "").repeat()
[0.33678007125854492, 0.33447504043579102, 0.33331012725830078]
>>> timeit.Timer("3 < 2", "").repeat()
[0.30328893661499023, 0.29070115089416504, 0.28839397430419922]

The problem with bit-bashing, masking etc. is that except for the most
simple cases it is quite obfuscated. If you aren't going to gain a serious
performance boost, why bother?




-- 
Steven D'Aprano 




More information about the Python-list mailing list