实际上,为什么比较整数比比较字符串更好? [英] In practice, why compare integer is better than compare string?
问题描述
我做了这个测试
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)
并获得结果
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?
推荐答案
简短的代码并不能告诉您太多信息。特别是,如果您查看 test1
和 test3
中的计时数字,则会看到数字是相同的。那应该是一个警告信号,实际上,您在这里看到的只是计时器的分辨率:
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
要获得更好的结果,您需要多次运行代码,然后测量并减去时序开销。 Python具有内置模块 timeit
做到这一点。让我们为每种比较的1亿次执行计时:
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
,所以您可以看到差异实际上并没有那么大(在这种情况下,字符串比较仅慢25%)。那么为什么字符串比较慢?好吧,找出方法就是查看比较操作的实现。
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.
在Python 2.7中,比较是通过 do_cmp
对象中的函数.c
。 (请在新窗口中打开此代码,以进行其余的分析。)在817行上,您将看到所比较的对象是否为相同类型,并且它们是否具有 tp_compare
函数的类结构,然后调用该函数。如果是整数对象,则会发生这种情况,该函数为 int_compare
在 intobject.c
中,您会发现这很简单。
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.
但是字符串没有 tp_compare
函数,因此 do_cmp
继续执行调用 try_rich_to_3way_compare
,然后调用 try_rich_compare_bool
(最多三遍)(依次尝试三个比较运算符EQ,LT和GT)。这会调用 try_rich_compare
,它会调用 string_richcompare
在 stringobject.c
中。
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.
这篇关于实际上,为什么比较整数比比较字符串更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!