Created
May 17, 2012 13:11
-
-
Save BrianHicks/2718811 to your computer and use it in GitHub Desktop.
Benchmark String Building
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
'test concatenation, substitution, join' | |
import timeit | |
DOMAIN = 'http://stackoverflow.com' | |
QUESTIONS = '/questsions' | |
def so_q_sub(n): | |
return '%s%s/%d' % (DOMAIN, QUESTIONS, n) | |
def so_q_cat(n): | |
return DOMAIN + QUESTIONS + '/' + str(n) | |
def so_q_join(n): | |
return ''.join([DOMAIN, QUESTIONS, '/', str(n)]) | |
if __name__ == '__main__': | |
print 'Substitution:' | |
print timeit.timeit('so_q_sub(1000)', 'from __main__ import so_q_sub') | |
print 'Concatentation:' | |
print timeit.timeit('so_q_cat(1000)', 'from __main__ import so_q_cat') | |
print 'Join:' | |
print timeit.timeit('so_q_join(1000)', 'from __main__ import so_q_join') | |
# output on my machine: | |
# Substitution: | |
# 1.0587220192 | |
# Concatenation: | |
# 0.573312997818 | |
# Join: | |
# 0.879829883575 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment