473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Algorithmic complexity of StringIO?

I want to build up a long string piece by piece without the quadratic
behavior of string concatenation. I'm looking at the {c,}StringIO
modules as a way around this, but I don't see anything in the docs which
talks about how they work. If I do

s = StringIO.StringIO()
while whatever:
s.write (stringFragment)
return s.getvalue()

will I see quadratic behavior? cStringIO claims to be more efficient,
but doesn't say how. Is it algorithmicly better, or just the same
algorithm recoded in C?

The context here is writing a __str__() method for a container class. I
could envision the containiner holding a couple thousand items, with
len(__str__()) being several 10's of kbytes.
Jul 18 '05 #1
2 2544
Roy Smith <ro*@panix.com> writes:
will I see quadratic behavior? cStringIO claims to be more efficient,
but doesn't say how. Is it algorithmicly better, or just the same
algorithm recoded in C?


A quick check of the write() method in StringIO shows that it
concatenates blocks of data onto a list (and presumably getvalue()
later does a ''.join()). So you don't get quadratic behaviour even
with StringIO.

Paul.
--
This signature intentionally left blank
Jul 18 '05 #2
Roy Smith wrote:
I want to build up a long string piece by piece without the quadratic
behavior of string concatenation. I'm looking at the {c,}StringIO
modules as a way around this, but I don't see anything in the docs which
talks about how they work. If I do

s = StringIO.StringIO()
while whatever:
s.write (stringFragment)
return s.getvalue()

will I see quadratic behavior? cStringIO claims to be more efficient,
but doesn't say how. Is it algorithmicly better, or just the same
algorithm recoded in C?

The context here is writing a __str__() method for a container class. I
could envision the containiner holding a couple thousand items, with
len(__str__()) being several 10's of kbytes.


Hello, Roy!

Common idiom is to use join method:

bunch_of_strings = ["aaa", ... "zzz"]

result = ''.join(bunch_of_strings)

If I'm correct, join preallocates needed space.

hth,
anton.

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Leif K-Brooks | last post by:
When StringIO gets an initial value passed to its constructor, it seems to discard it after the first call to .write(). For instance: >>> from StringIO import StringIO >>> buffer =...
4
by: Generic Usenet Account | last post by:
Consider two entities A and B such that there is a 1:n association between them. I mean that associated with each instance of A there are up to n instances of B. Currently in our software we are...
21
by: Paul Rubin | last post by:
I've always found the string-building idiom temp_list = for x in various_pieces_of_output(): v = go_figure_out_some_string() temp_list.append(v) final_string = ''.join(temp_list) ...
3
by: Max | last post by:
I'm using StringIO for the first time (to buffer messages recieved from a socket). I thought it would be a simple matter of writing the stuff to the buffer and then calling readline, but that...
4
by: Xah Lee | last post by:
i've long time been interested in algorithmic mathematical art. That is, mathematical or algorithmic visual art works that are generated by computer such that the program's source code reflects the...
2
by: Jonathan Bowlas | last post by:
Hi listers, I've written this little script to generate some html but I cannot get it to convert to a string so I can perform a replace() on the &gt;, &lt; characters that get returned. from...
3
by: bob | last post by:
I'm using the code below to read the zipped, base64 encoded WMF file saved in an XML file with "Save as XML" from MS Word. As the "At this point" comment shows, I know that the base64 decoding is...
6
by: sebastian.noack | last post by:
Hi, is there a way to or at least a reason why I can not use tarfile to create a gzip or bunzip2 compressed archive in the memory? You might might wanna answer "use StringIO" but this isn't...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.