Can you explain the performance of in()?

Refresh

February 2019

Views

36 time

0

I'm going through the docs.python.org tutorial and I got curious when I saw in one of their examples (4.7.1 to be precise), that instead of checking if the string 'nope' starts with the user's raw input, they call the function in('n', 'no', 'nop', 'nope'). I was wandering if it performs better. So I wrote the following code to measure it:

import time

nope = "nope"
nopes = [nope[:1], nope[:2], nope[:3], nope]
bope = "bope"
no = "no"
st = time.time()
y = 10 ** 6
for i in range(0, y):
    bope in nopes
print "bope in nopes " + str(time.time() - st)
st = time.time()
for i in range(0, y):
    nope.startswith(bope)
print "nope startswith bope " + str(time.time() - st)
st = time.time()
for i in range(0, y):
    no in nopes
print "no in nopes " + str(time.time() - st)
st = time.time()
for i in range(0, y):
    nope.startswith(no)
print "nope startswith no " + str(time.time() - st)

And I got these results:

bope in nopes 0.285495996475
nope startswith bope 0.579398155212
no in nopes 0.266932010651
nope startswith no 0.58361697197

  1. It doesn't seem to matter if it's a match case or not
  2. in() performs much better

Can anyone explain this?

0 answers