Created
April 22, 2013 13:51
-
-
Save TimidRobot/5435216 to your computer and use it in GitHub Desktop.
String concatenation comparison Also see: - http://greaterdebater.com/blog/gabe/post/7
- http://www.skymind.com/~ocrow/python_string/
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
#!/usr/bin/env python | |
""" | |
String concatenation comparison | |
Also see: | |
- http://greaterdebater.com/blog/gabe/post/7 | |
- http://www.skymind.com/~ocrow/python_string/ | |
""" | |
# Standard library | |
import commands | |
import locale | |
import os | |
from sys import argv | |
import timeit | |
def method1(): | |
out_str = "" | |
for num in xrange(loops): | |
out_str += repr(num) | |
ps_stats() | |
return out_str | |
def method2(): | |
from UserString import MutableString | |
out_str = MutableString() | |
for num in xrange(loops): | |
out_str += repr(num) | |
ps_stats() | |
return out_str | |
def method3(): | |
from array import array | |
char_array = array("c") | |
for num in xrange(loops): | |
char_array.fromstring(repr(num)) | |
out_str = char_array.fromstring(repr(num)) | |
ps_stats() | |
return out_str | |
def method4(): | |
str_list = [] | |
for num in xrange(loops): | |
str_list.append(repr(num)) | |
out_str = "".join(str_list) | |
ps_stats() | |
return out_str | |
def method5(): | |
from cStringIO import StringIO | |
file_str = StringIO() | |
for num in xrange(loops): | |
file_str.write(repr(num)) | |
ps_stats() | |
return file_str.getvalue() | |
def method6(): | |
out_str = "".join([repr(num) for num in xrange(loops)]) | |
ps_stats() | |
return out_str | |
def method7(): | |
out_str = "" | |
for num in xrange(loops): | |
out_str = "%s%s" % (out_str, num) | |
ps_stats() | |
return out_str | |
def ps_stats(): | |
global process_size | |
ps = commands.getoutput("ps -o rss -p %s" % pid) | |
process_size = ps.split()[1] | |
def call_method(num): | |
global process_size | |
method = "method%s" % str(num) | |
time = timeit.timeit("%s()" % method, | |
setup="from __main__ import %s" % method, | |
number=1) | |
print "method%s, %6.3f seconds, %7s KB" % (num, time, process_size) | |
locale.setlocale(locale.LC_ALL, 'en_US') | |
process_size = "" | |
try: | |
loops = int(argv[1]) | |
except: | |
loops = 1000000 | |
pid = os.getpid() | |
print "Loops: %s\n" % locale.format("%d", loops, grouping=True) | |
for i in xrange(1, 8): | |
if i in (2, 7) and loops >= 100000: | |
continue | |
call_method(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment