Skip to content

Instantly share code, notes, and snippets.

@5j9
Last active May 30, 2018 23:42
Show Gist options
  • Save 5j9/c75d984cede3c0280788a6f27b69d9f9 to your computer and use it in GitHub Desktop.
Save 5j9/c75d984cede3c0280788a6f27b69d9f9 to your computer and use it in GitHub Desktop.
timings for a is_mixed_case(string) function
from timeit import timeit
name = 'common_case'
def f0(name): # 0.38833552993028425
stripped_name = name.strip('_')
tail = stripped_name[1:]
return stripped_name[:1].islower() and tail.lower() != tail
def f1(name): # 0.5023811628120246
stripped_name = name.strip('_')
head = stripped_name[:1]
tail = stripped_name[1:]
return head.islower() and bool(tail) and not (tail.islower() or tail.isdigit())
def f2(name): # 1.2597934653988185
stripped_name = name.strip('_')
head_is_lower = stripped_name[:1].islower()
return head_is_lower and any(map(str.isupper, stripped_name[1:]))
def f3(name): # 1.399307801830291
stripped_name = name.strip('_')
head_is_lower = stripped_name[:1].islower()
return head_is_lower and any(c.isupper() for c in stripped_name[1:])
def f4(name): # 0.9169683581987003
stripped_name = name.strip('_')
if stripped_name[:1].islower():
for c in stripped_name:
if c.isupper():
return True
def f5(name): # 0.37668225294656565
stripped_name = name.lstrip('_')
tail = stripped_name[1:]
return stripped_name[:1].islower() and tail.lower() != tail
def f6(name): # 0.32481864568646446
return name.lstrip('_')[:1].islower() and name.lower() != name
print(timeit('f0(name)', globals=globals()))
print(timeit('f1(name)', globals=globals()))
print(timeit('f2(name)', globals=globals()))
print(timeit('f3(name)', globals=globals()))
print(timeit('f4(name)', globals=globals()))
print(timeit('f5(name)', globals=globals()))
print(timeit('f6(name)', globals=globals()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment