cStringIO rules (was Re: O(n^2) is bad - can it be fixed?)
Alex Martelli
aleaxit at yahoo.com
Tue May 22 04:50:44 EDT 2001
"Chris Tavares" <ctavares at develop.com> wrote in message
news:aVnO6.8300$9D5.805877 at newsread2.prod.itd.earthlink.net...
...
> Another solution would be to use something that IS expected to act like a
> buffer - the cStringIO module. The interface is a little different - this
> one works like a file:
Good point! So I added it to my little measurement script:
def AppendTest(listorarray):
head = repr(listorarray)
start = time.clock()
for i in range(0, 100):
for x in range(0, 2000):
listorarray.append(' ')
# print i
stend = time.clock()
print "%s: %.2f" % (head, stend-start)
def AppendTest1(listorarray):
head = repr(listorarray)
start = time.clock()
for i in range(0, 100):
for x in range(0, 2000):
listorarray.write(' ')
# print i
stend = time.clock()
print "%s: %.2f" % (head, stend-start)
AppendTest([])
AppendTest(array.array('c'))
AppendTest1(cStringIO.StringIO())
and, whaddyaknow...:
D:\py21>python oaat.py
[]: 1.96
array('c'): 1.57
<StringO object at 007F63B0>: 0.86
D:\py21>python oaat.py
[]: 1.96
array('c'): 1.57
<StringO object at 007F63B0>: 0.87
D:\py21>
Bingo, another "almost a factor of 2" speedup.
Interesting!-) Guido's optimization anecdote
may need updating:-).
Alex
More information about the Python-list
mailing list