Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mahmoudimus/297316 to your computer and use it in GitHub Desktop.
Save mahmoudimus/297316 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
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