Skip to content

Instantly share code, notes, and snippets.

@smcl
Last active July 31, 2020 16:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smcl/7462529818bb77baad32727a9e5ff44c to your computer and use it in GitHub Desktop.
Save smcl/7462529818bb77baad32727a9e5ff44c to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from cStringIO import StringIO
import time, commands, os
from sys import argv
def method1():
out_str = ''
for num in xrange(loop_count):
out_str += `num`
ps_stats()
return out_str
def method2():
from UserString import MutableString
out_str = MutableString()
for num in xrange(loop_count):
out_str += `num`
ps_stats()
return out_str
def method3():
from array import array
char_array = array('c')
for num in xrange(loop_count):
char_array.fromstring(`num`)
ps_stats()
return char_array.tostring()
def method4():
str_list = []
for num in xrange(loop_count):
str_list.append(`num`)
out_str = ''.join(str_list)
ps_stats()
return out_str
def method5():
file_str = StringIO()
for num in xrange(loop_count):
file_str.write(`num`)
out_str = file_str.getvalue()
ps_stats()
return out_str
def method6():
out_str = ''.join([`num` for num in xrange(loop_count)])
ps_stats()
return out_str
def ps_stats():
global process_size
ps = commands.getoutput('ps -up ' + `pid`)
process_size = ps.split()[15]
def call_method(num):
global process_size
start = time.time()
z = eval('method' + str(num))()
end = time.time()
print "method", num
print "time", float((end-start) * 1000), "ms"
print "output size ", len(z) / 1024, "kb"
print "process size", process_size, "kb"
print
loop_count = 20000
pid = os.getpid()
if len(argv) == 2:
call_method(argv[1])
else:
print "Usage: python stest.py <n>\n" \
" where n is the method number to test"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment