Created
January 25, 2014 00:54
-
-
Save twonds/8610029 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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