Tricks to do "enums"?
Gareth McCaughan
Gareth.McCaughan at pobox.com
Mon May 8 15:49:02 EDT 2000
Neil Schemenauer wrote:
> "==" works just as well. Python smart enough to try a pointer
> compare first. The code path is almost the same so I doubt there
> would be much speed gained by using "is".
Er, surely it will fall back on a string comparison if
the two aren't pointer-equal?
I think concern about efficiency is misplaced here,
in any case.
>>> import time
>>> x = range(500000)
>>> y = map(str, x)
>>> def test_i():
... t0 = time.time()
... u = map(lambda t: t==5000, x)
... t1 = time.time()
... return t1-t0
...
>>> def test_s():
... t0 = time.time()
... u = map(lambda t: t=='5000', x)
... t1 = time.time()
... return t1-t0
...
>>> test_i()
1.45546197891
>>> test_i()
1.34740400314
>>> test_s()
1.71845293045
>>> test_s()
1.70223104954
>>> 1.702/1.347
1.26354862658
If the difference in speed is only 25% here, it's unlikely
to be a big deal in any real program. (I don't mean that the
cost of a string compare is only 25% more than that of an
integer compare; that's not the point.)
--
Gareth McCaughan Gareth.McCaughan at pobox.com
sig under construction
More information about the Python-list
mailing list