Skip to content

Instantly share code, notes, and snippets.

@1st1
Created February 1, 2016 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1st1/10588e6e11c4d7c19445 to your computer and use it in GitHub Desktop.
Save 1st1/10588e6e11c4d7c19445 to your computer and use it in GitHub Desktop.
import time
def sub1(s):
s[:3] == 'abc'
s[:3] == 'abc'
s[:3] == 'abc'
s[:3] == 'abc'
s[:3] == 'abc'
s[:3] == 'abc'
s[:3] == 'abc'
s[:3] == 'abc'
s[:3] == 'abc'
s[:3] == 'abc'
s[:3] == 'abc'
s[:3] == 'abc'
s[:3] == 'abc'
s[:3] == 'abc'
s[:3] == 'abc'
def sub2(s):
s.startswith('abc')
s.startswith('abc')
s.startswith('abc')
s.startswith('abc')
s.startswith('abc')
s.startswith('abc')
s.startswith('abc')
s.startswith('abc')
s.startswith('abc')
s.startswith('abc')
s.startswith('abc')
s.startswith('abc')
s.startswith('abc')
s.startswith('abc')
s.startswith('abc')
def test():
s = 'abcdef'
t = time.monotonic()
for _ in range(10**7):
sub1(s)
print('s[:3]', time.monotonic() - t)
t = time.monotonic()
for _ in range(10**7):
sub2(s)
print('s.startswith()', time.monotonic() - t)
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment