Use of StringIO vs cStringIO in standard modules

Guido van Rossum guido at CNRI.Reston.VA.US
Thu Jun 3 10:25:03 EDT 1999

Hrvoje Niksic <hniksic at>:

> I noticed that many standard modules use StringIO and not cStringIO,
> although they don't need subclassing.  Is this intentional?
> For example, uses StringIO to implement encodestring() and
> decodestring().  Since both functions write to output line by line, I
> imagine the performance hit of StringIO vs cStringIO might be
> non-negligible.

Have you noticed any speed difference?  cStringIO is important for
cPickle, because calling it from C is much faster than calling
StringIO from C; however I believe that for calls from Python,
StringIO isn't that much slower.  (This as opposed to cPickle
vs. Pickle, where the performance difference is 2-3 orders of

> Furthermore, is there a particular reason for maintaining two parallel
> StringIO implementations?  If subclassing is the reason, I assume it
> would be trivial to rewrite StringIO to encapsulate cStringIO the same 
> way that UserDict encapsulates dictionary objects.

That's not the reason; it's got more to do with not requiring a C
extension where plain Python code will do.  Also to have a reference
implementation.  (Ditto for pickle.)

--Guido van Rossum (home page:

More information about the Python-list mailing list