better way than: myPage += 'more html' , ...
Adrien Di Mascio
Adrien.DiMascio at logilab.fr
Thu Jun 26 09:33:58 EDT 2003
> Your usage of "should" is Interesting :-). I wonder what it really means;
> either that you did not time it, or that you timed it and found it not to be
> the fastest. I'm a big fan of the `.append'-based method, but would switch
> to `cStringIO' if it was significantly faster[1]. I guess that both methods
> are roughly O(N), but would suspect the `cStringIO' might have a higher
> start factor, making it less appealing for only a few short strings. Did
> someone actually timed both methods compared, and got an opinion?
>
I've a made a quite basic test on these methods :
def write_thousands_chars(self, char):
"""Writes a 100000 times 'char'
"""
res = ""
for index in range(100000):
res += char
return res
def write_thousands_cstringio(self, char):
"""Writes a 100000 times 'char' in a cStringIO
"""
from cStringIO import StringIO
res = StringIO()
for index in range(100000):
res.write(char)
return res
def write_thousands_stringio(self, char):
"""Writes a 100000 times 'char' in a cStringIO
"""
from StringIO import StringIO
res = StringIO()
for index in range(100000):
res.write(char)
return res
def write_thousands_join(self, char):
"""Writes a 100000 times 'char' in a list and joins it
"""
str_list = []
for index in range(100000):
str_list.append(char)
return ''.join(str_list)
Here's the result :
*************** Results ***************
In <__main__.MyClass instance at 0x8122e54> :
write_thousands_cstringio (called 1 times):
- Called at Thu Jun 26 15:30:36 2003, exec_time =
0.177428007126 sec
write_thousands_chars (called 1 times):
- Called at Thu Jun 26 15:30:05 2003, exec_time =
29.1778309345 sec
write_thousands_join (called 1 times):
- Called at Thu Jun 26 15:30:36 2003, exec_time =
0.16690993309 sec
write_thousands_stringio (called 1 times):
- Called at Thu Jun 26 15:30:35 2003, exec_time =
0.979285955429 sec
*****************************************
cStringIO and join give silimar results, but on smaller strings,
the "join()" way seems better.
--
Adrien Di Mascio
LOGILAB, Paris (France).
http://www.logilab.com http://www.logilab.fr http://www.logilab.org
More information about the Python-list
mailing list