Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save justinvanwinkle/313120 to your computer and use it in GitHub Desktop.
Save justinvanwinkle/313120 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from cStringIO import StringIO
from UserString import MutableString
data = xrange(100000)
def test_join_way():
return ''.join([str(x) for x in data])
def test_stringio_way():
f = StringIO()
for x in data:
f.write(str(x))
return f.getvalue()
def test_generator_join_way():
return ''.join((str(x) for x in data))
def test_naive_way():
accum = ''
for x in data:
accum += str(x)
return accum
def test_join_for_way():
accum = []
for x in data:
accum.append(str(x))
return ''.join(accum)
def _test_mutable_way():
accum = MutableString()
for x in data:
accum += str(x)
return accum
if __name__ == '__main__':
from timeit import Timer
itercnt = 1000
expression = "%s()"
impst = "from __main__ import %s, StringIO, data, MutableString"
funcs = [x for x in globals().keys() if x.startswith('test_')]
for f in funcs:
t = Timer(expression % f, impst % f)
print f, t.timeit(itercnt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment