Skip to content

Instantly share code, notes, and snippets.

@twonds
Created January 25, 2014 00:54
Show Gist options
  • Save twonds/8610029 to your computer and use it in GitHub Desktop.
Save twonds/8610029 to your computer and use it in GitHub Desktop.
import time
import random
import string
TEST_STR = "".join([string.letters[random.randint(1, len(string.letters)-1)] for x in range(0, 10000)])
start_time = time.time()
TEST_STR.upper()
print "UPPER", time.time() - start_time
start_time = time.time()
upper_string = ""
for c in TEST_STR:
continue
print "LOOP", time.time() - start_time
start_time = time.time()
upper_string = ""
for c in TEST_STR:
upper_string += c.upper()
print "UPPER LOOP", time.time() - start_time
start_time = time.time()
upper_string = ""
for c in TEST_STR:
upper_string += c.capitalize()
print "CAPITALIZE LOOP", time.time() - start_time
CHAR_MAP = dict([(key, string.uppercase[string.lowercase.index(key)]) for key in string.lowercase])
start_time = time.time()
upper_string = ""
for c in TEST_STR:
upper_string += CHAR_MAP.get(c, c)
print "CHECK INDEX LOOP", time.time() - start_time
start_time = time.time()
upper_string = ""
for c in TEST_STR:
ord_c = ord(c)
if ord_c < 91:
upper_string += unichr(ord_c + 32)
else:
upper_string += c
print "ORD IT LOOP", time.time() - start_time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment