Susanne Moelbert wrote:
> Hi,
>
> I was very amazed yesterday when I wanted to calculate a simple thing
> with Python. I typed
>
> for a in range(200):
> for b in range(200):
> for c in range(200):
> pass
>
> and it took over 10 seconds! I typed the same thing in Borland Delphi
> (on the same machine, a Pentium III) and it was immediate. One wouldn't
> even realize that it was calculating. In C++ it was slightly slower than
> in Delphi, but by far not as slow as Python. Why is it so terribly slow,
> even if it doesn't need to calculate anything? Is there a possibility
> for accelerating this?
Well, I guess the situation is this:
Delphi trys to optimize the code at compile time. It recognizes, that
there is really nothing to do, so the whole loop construct is optimized
away.
C++ would probably be as fast as Delphi if you compile with -O3 or
something like this.
Python does no optimizations, so Python is really performing the loops,
even if there is nothing to do. So in a real world example there would of
course be something to do in the inner loop, so Delphi and C++ won't return
emmediate. The other point is that Python is interpreted (even if the
script is translated to bytecode before), and scripting languages tend to
be 20 to 30 times slower than compiled ones. But if you want short
turn-around times, and want to have fun during programming, ...
Markus
|