Python is faster than C

Andrew Dalke adalke at mindspring.com
Sun Apr 4 13:40:20 EDT 2004


Armin Rigo:
> Another example would be 'a'*999999999: the result is a string, but
> there is no reason that it takes 100MB of memory.  Instead, store it
> into a C structure that contains a pointer to the original string object
> 'a' and the repetition counter, but still give this C structure the
> Python type str, so that the difference doesn't show up and the Python
> language remains simple.

I've written lazy code like this for my own projects, where the
concrete object wasn't fully created until needed.  One of the
problems I had with it was error behaviour.  In this case, suppose
there isn't enough memory available.  Python as is will attempt to
allocate enough space when you request it and raise a MemoryError
if there isn't enough space.

Python as you propose it may allocate the string-like object and
only later (eg, when passing the object to some external C
function which expects a char *) realize the full string.  There
isn't enough memory so the allocation fails, raising a MemoryError.
But how is the programmer to realize which operations may raise
a MemoryError?  Instead, will everything need a try/except guard
around it?

                    Andrew
                    dalke at dalkescientific.com





More information about the Python-list mailing list