Last active
December 18, 2015 16:49
-
-
Save maizy/5814102 to your computer and use it in GitHub Desktop.
join str with cStringIO vs ''.join(); ipython -i buf_vs_join.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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())') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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())') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ipython -i buf_vs_join.py
ipython -i buf_vs_join_for_iter.py
ipython-2.7 -i buf_vs_join.py
ipython-2.7 -i buf_vs_join_for_iter.py