Skip to content

Instantly share code, notes, and snippets.

@mopemope
Created September 29, 2009 06:37
Show Gist options
  • Save mopemope/196450 to your computer and use it in GitHub Desktop.
Save mopemope/196450 to your computer and use it in GitHub Desktop.
import timeit
from cStringIO import StringIO
x1 = xrange(1000)
x2 = xrange(10000)
smallbuf = "buffer"
bigbuf = "buffer" * 1024
def str_join1000(buf):
lst = []
for i in x1:
lst.append(buf)
return ''.join(lst)
def string_io1000(buf):
io = StringIO()
write = io.write
for i in x1:
write(buf)
return io.getvalue()
def map1000(buf):
lst = map(lambda x:buf, x1)
return ''.join(lst)
def str_join10000(buf):
lst = []
for i in x2:
lst.append(buf)
return ''.join(lst)
def string_io10000(buf):
io = StringIO()
write = io.write
for i in x2:
write(buf)
return io.getvalue()
def map10000(buf):
lst = map(lambda x:buf, x2)
return ''.join(lst)
res = timeit.timeit("str_join1000(buf)", "from __main__ import str_join1000, smallbuf as buf", number=1000)
print "small string_join x 1000: %f" % res
res = timeit.timeit("string_io1000(buf)", "from __main__ import string_io1000, smallbuf as buf", number=1000)
print "small string_io x 1000: %f" % res
res = timeit.timeit("map1000(buf)", "from __main__ import map1000, smallbuf as buf", number=1000)
print "small map x 1000: %f" % res
res = timeit.timeit("str_join10000(buf)", "from __main__ import str_join10000, smallbuf as buf", number=1000)
print "small string_join x 10000: %f" % res
res = timeit.timeit("string_io10000(buf)", "from __main__ import string_io10000, smallbuf as buf", number=1000)
print "small string_io x 10000: %f" % res
res = timeit.timeit("map10000(buf)", "from __main__ import map10000, smallbuf as buf", number=1000)
print "small map x 10000: %f" % res
res = timeit.timeit("str_join1000(buf)", "from __main__ import str_join1000, bigbuf as buf", number=1000)
print "big string_join x 1000: %f" % res
res = timeit.timeit("string_io1000(buf)", "from __main__ import string_io1000, bigbuf as buf", number=1000)
print "big string_io x 1000: %f" % res
res = timeit.timeit("map1000(buf)", "from __main__ import map1000, bigbuf as buf", number=1000)
print "big map x 1000: %f" % res
res = timeit.timeit("str_join10000(buf)", "from __main__ import str_join10000, bigbuf as buf", number=1000)
print "big string_join x 10000: %f" % res
res = timeit.timeit("string_io10000(buf)", "from __main__ import string_io10000, bigbuf as buf", number=1000)
print "big string_io x 10000: %f" % res
res = timeit.timeit("map10000(buf)", "from __main__ import map10000, bigbuf as buf", number=1000)
print "big map x 10000: %f" % res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment