Is Python really slow?

Cedric Adjih adjih at crepuscule.com
Mon May 29 08:16:09 EDT 2000


Alexander <alecdem at mit.edu> wrote:
> Michael Hudson wrote:
>> Alexander <alecdem at mit.edu> writes:
>>
>> > Hi
>> >
>> > I have implemented  loop in Python
>> >
>> > for k in xrange(1,100000000):
>> >    pass
>> >
>> > and the same one in  C
>> > for ( i = 0 ; i < 1e8; i++ ) ;
>> >
>> > and the second one seems to be working hundreds of times faster than the
>> > first one
>> >
>> > Can I do anything with that.
>>
>> Well, it's an empty loop so it's pretty useless to all mankind.
>>
>> Is this /just/ a troll, or has Python's (relative) lack of speed
>> bothered you?  Yes, C can spin empty loops faster (especially if the
>> optimiser removes the loop entirely...), but for doing most
>> significant things, the difference is much less noticeable.
>
> Actually I've created array and did something like this :
>
> for k in xrange(1,10000):
>   for m in range(1,10000):
>     x[m] =  .... #some calculation of the value of an array's element x[m]
>
> and this work's realy slow compared to C. I don't mind it working 3 times
> slower, but ..
>
> Perhaps it is possible, to do something like  implementing loops in C inside
> Python ??
> (though it seems to me it will not help)

  You're right, Python is slow. That is fast to program with, but slow
to run. For many cases, it is fast enough.
For other cases, its speed is an issue, but programmer time is deemed
more important.
For other cases, one can profile, and write a C module (with the
extension API, SWIG, CXX or SCXX), for the bottlenecks, when possible.

If you need more speed than Python has (but not top speed), and something
more clean than C++, you can try something in between, such
as Java or Eiffel.

-- Cedric



More information about the Python-list mailing list