In Practice, Why Compare Integer Is Better Than Compare String?
Solution 1:
Timing a single execution of a short piece of code doesn't tell you very much at all. In particular, if you look at the timing numbers from your test1
and test3
, you'll see that the numbers are identical. That ought to be a warning sign that, in fact, all that you're seeing here is the resolution of the timer:
>>>2.0 / 2 ** 20
1.9073486328125e-06
>>>1.0 / 2 ** 20
9.5367431640625e-07
For better results, you need to run the code many times, and measure and subtract the timing overhead. Python has a built-in module timeit
for doing exactly this. Let's time 100 million executions of each kind of comparison:
>>>from timeit import timeit>>>timeit('100 > 200', number=10**8)
5.98881983757019
>>>timeit('"100" > "200"', number=10**8)
7.528342008590698
so you can see that the difference is not really all that much (string comparison only about 25% slower in this case). So why is string comparison slower? Well, the way to find out is to look at the implementation of the comparison operation.
In Python 2.7, comparison is implemented by the do_cmp
function in object.c
. (Please open this code in a new window to follow the rest of my analysis.) On line 817, you'll see that if the objects being compared are the same type and if they have a tp_compare
function in their class structure, then that function is called. In the case of integer objects, this is what happens, the function being int_compare
in intobject.c
, which you'll see is very simple.
But strings don't have a tp_compare
function, so do_cmp
proceeds to call try_rich_to_3way_compare
which then calls try_rich_compare_bool
up to three times (trying the three comparison operators EQ, LT and GT in turn). This calls try_rich_compare
which calls string_richcompare
in stringobject.c
.
So string comparison is slower because it has to use the complicated "rich comparison" infrastructure, whereas integer comparison is more direct. But even so, it doesn't make all that much difference.
Solution 2:
Huh? Since the storage space is reduced, the number of bits that need to be compared is also reduced. Comparing bits is work, doing less work means it goes faster.
Post a Comment for "In Practice, Why Compare Integer Is Better Than Compare String?"