Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save justinvanwinkle/297317 to your computer and use it in GitHub Desktop.
Save justinvanwinkle/297317 to your computer and use it in GitHub Desktop.
fix top
#!/usr/bin/env python
import time
data = range(10000000)
def test_method(method):
start_time = time.time()
result = method()
assert len(result) == 68888890, 'Method work no good: %s != 68888890' % len(result)
end_time = time.time()
print method.func_name, end_time - start_time
def naive_way():
accum = ''
for x in data:
accum += str(x)
return accum
def join_way():
return ''.join([str(x) for x in data])
def join_for_way():
accum = []
for x in data:
accum.append(str(x))
return ''.join(accum)
from UserString import MutableString
def mutable_way():
accum = MutableString()
for x in data:
accum += str(x)
return accum
from cStringIO import StringIO
def stringio_way():
f = StringIO()
for x in data:
f.write(str(x))
return f.getvalue()
test_method(naive_way)
test_method(join_way)
test_method(join_for_way)
#test_method(mutable_way)
test_method(stringio_way)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment