In practice, why compare integer is better than compare string?

倖福魔咒の 提交于 2019-12-24 08:01:49

问题


I did this test

import time
def test1():
    a=100
    b=200
    start=time.time()
    if (a>b):
        c=a
    else:
        c=b
    end=time.time()
    print(end-start)


def test2():
    a="amisetertzatzaz1111reaet"
    b="avieatzfzatzr333333ts"
    start=time.time()

    if (a>b):
        c=a
    else:
        c=b
    end=time.time()
    print(end-start)

 def test3():
     a="100"
     b="200"
     start=time.time()

     if (a>b):
         c=a
     else:
         c=b
     end=time.time()
     print(end-start)

And obtain as result

1.9073486328125e-06    #test1()
9.5367431640625e-07    #test2()
1.9073486328125e-06    #test3()

Execution times are similar. It's true, use integer instead of string reduce the storage space but what about the execution time?


回答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.




回答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.



来源:https://stackoverflow.com/questions/14400018/in-practice-why-compare-integer-is-better-than-compare-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!