Skip to content

Instantly share code, notes, and snippets.

@apiguy
Last active December 19, 2015 16:39
Show Gist options
  • Save apiguy/5985412 to your computer and use it in GitHub Desktop.
Save apiguy/5985412 to your computer and use it in GitHub Desktop.
Comparison of performance of case sensitivity comparisons in python
import timeit
setup = """
import re
from itertools import ifilter
x = "active"
regex = re.compile(x, re.IGNORECASE)
def uppertest():
return x.upper() == "ACTIVE"
def lowertest():
return x.lower() == "active"
def intest():
return x in ["ACTIVE", "active"]
def ortest():
return x == "ACTIVE" or x == "active"
def regextest():
return regex.match("ACTIVE")
def base():
return x == "active"
"""
print "upper test:", min(timeit.Timer("uppertest()", setup=setup).repeat(7, 1000))
print "lower test:", min(timeit.Timer("lowertest()", setup=setup).repeat(7, 1000))
print "in test: ", min(timeit.Timer("intest()", setup=setup).repeat(7, 1000))
print "or test: ", min(timeit.Timer("ortest()", setup=setup).repeat(7, 1000))
print "regex test:", min(timeit.Timer("regextest()", setup=setup).repeat(7, 1000))
print "baseline: ", min(timeit.Timer("base()", setup=setup).repeat(7, 1000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment