Python is faster than C

Stephen Horne steve at ninereeds.fsnet.co.uk
Tue Apr 6 12:10:25 EDT 2004


On 03 Apr 2004 18:23:11 -0800, Paul Rubin
<http://phr.cx@NOSPAM.invalid> wrote:

>Armin Rigo <arigo at tunes.org> writes:
>> Ideally: If you do  x=range(100); x[50]='hi'  then the interpreter first
>> builds this optimized range representation and assigns it to x; and when
>> in the next statement you modify this list x it says 'oops! i cannot do
>> that with this representation', so it reverts to an array-like
>> representation (i.e. it creates all 100 elements) and then changes the
>> 50th.  No gain here.  If on the other hand you only ever do 'easy'
>> things with your list, like iterate over it or read elements, then it
>> can all be done with the range representation, without falling back to
>> the array representation.
>
>Maybe there is something to this.  

The problem is, once you start where do you stop.

At the moment, Armin suggests optimising the a list consisting of a
single repeating item. But what about optimising a repeating list with
one or two special cases, so that the "x[50]='hi'" above doesn't incur
a penalty?

Consider integer lists - what about optimising arithetic progressions?
Geometric progressions? Progressions with special cases? Progressions
that are the intersection, or union, or whatever of other
progressions?

If the internal implementation doesn't special-case the implementation
of operations on these, all you have is lazy evaluation. But if the
internal implementation adds special-case implementations of
operations, you either end up with an huge number of special case
implementation methods (other parameters can end up with special-case
implementations, not just 'self') or you have to implement what
amounts to a full algebraic solving engine in the Python interpreter.

Or else you can just choose to special case the really important types
and operations, which I believe Python already does to some degree (an
integer is a special case of a long integer, for instance, and an
iterator is a special case of a list with only a subset of the
operations available to a standard list) and provide the programmer
with the tools to easily implement any further special cases that he
may need.


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk



More information about the Python-list mailing list