Skip to content

Instantly share code, notes, and snippets.

@maizy
Last active December 18, 2015 16:49
Show Gist options
  • Save maizy/5814102 to your computer and use it in GitHub Desktop.
Save maizy/5814102 to your computer and use it in GitHub Desktop.
join str with cStringIO vs ''.join(); ipython -i buf_vs_join.py
# coding: utf-8
from cStringIO import StringIO
def test_buf(i):
buf = StringIO()
for v in i:
buf.write(v)
return buf.getvalue()
def test_join(i):
list_res = []
for v in i:
list_res.append(v)
return ''.join(list_res)
def test_iter():
for i in xrange(1000000):
yield str(i)
assert test_join(test_iter()) == test_buf(test_iter())
print 'Test bufer'
get_ipython().magic(u'timeit test_buf(test_iter())')
print 'Test join'
get_ipython().magic(u'timeit test_join(test_iter())')
# coding: utf-8
from cStringIO import StringIO
def test_buf(i):
buf = StringIO()
for v in i:
buf.write(v)
return buf.getvalue()
def test_join(i):
return ''.join(i)
def test_iter():
for i in xrange(1000000):
yield str(i)
assert test_join(test_iter()) == test_buf(test_iter())
print 'Test bufer'
get_ipython().magic(u'timeit test_buf(test_iter())')
print 'Test join'
get_ipython().magic(u'timeit test_join(test_iter())')
@maizy
Copy link
Author

maizy commented Jun 19, 2013

ipython -i buf_vs_join.py

Python 2.6.8 (unknown, Nov 17 2012, 21:23:35) 

Test bufer
1 loops, best of 3: 557 ms per loop
Test join
1 loops, best of 3: 449 ms per loop

ipython -i buf_vs_join_for_iter.py

Python 2.6.8 (unknown, Nov 17 2012, 21:23:35) 

Test bufer
1 loops, best of 3: 625 ms per loop
Test join
1 loops, best of 3: 337 ms per loop

ipython-2.7 -i buf_vs_join.py

Python 2.7.5 (default, May 19 2013, 13:26:46) 

Test bufer
1 loops, best of 3: 842 ms per loop
Test join
1 loops, best of 3: 450 ms per loop

ipython-2.7 -i buf_vs_join_for_iter.py

Python 2.7.5 (default, May 19 2013, 13:26:46) 

Test bufer
1 loops, best of 3: 802 ms per loop
Test join
1 loops, best of 3: 289 ms per loop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment