简体   繁体   中英

Why string comparison is NOT faster then integer comparison in Python?

The difference in C++ is huge, but not in Python. I used similar code on C++, and the result is so different -- integer comparison is 20-30 times faster than string comparison.

Here is my example code:

import random, time
rand_nums = []
rand_strs = []
total_num = 1000000
for i in range(total_num):
    randint = random.randint(0,total_num*10)
    randstr = str(randint)
    rand_nums.append(randint)
    rand_strs.append(randstr)

start = time.time()
for i in range(total_num-1):
    b = rand_nums[i+1]>rand_nums[i]
end = time.time()
print("integer compare:",end-start)     # 0.14269232749938965 seconds

start = time.time()
for i in range(total_num-1):
    b = rand_strs[i+1]>rand_strs[i]
end = time.time()                       # 0.15730643272399902 seconds
print("string compare:",end-start)

I can't explain why it's so slow in C++, but in Python, the reason is simple from your test code: random strings usually differ in the first byte, so the comparison time for those cases should be pretty much the same.

Also, not that much of your overhead will be in the loop control and list accesses. You'd get a much more accurate measure if you remove those factors by zip ping the lists:

for s1, s2 in zip(rand_strs, rand_strs[1:]):
    b = s1 > s2

The difference in C++ is huge, but not in Python.

The time spent in the comparison is minimal compared to the rest of the loop in Python. The actual comparison operation is implemented in Python's standard library C code, while the loop will execute through the interpreter.

As a test, you can run this code that performs all the same operations as the string comparison loop, except without the comparison:

start = time.time()
for i in range(total_num-1):
    b = rand_strs[i+1], rand_strs[i]
end = time.time()
print("no compare:",end-start)

The times are pretty close to each other, though for me string comparison is always the slowest of the three loops:

integer compare: 1.2947499752044678
string compare: 1.3821675777435303
no compare: 1.3093421459197998

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM