Why is this loop heavy code so slow in Python? Possible Project Euler spoilers

Ivan Wang ivanwangcn at gmail.com
Sun Sep 2 16:28:19 CEST 2007

On Sep 2, 9:45 pm, jwrweather... at gmail.com wrote:
> [snip code]
> Thanks for that. I realise that improving the algorithm will speed
> things up. I wanted to know why my less than perfect algorithm was so
> much slower in python than exactly the same algorithm in C. Even when
> turning off gcc's optimiser with the -O0 flag, the C version is still
> > 100 times quicker.- Hide quoted text -
> - Show quoted text -
Maybe Python is  the slowest programming language in the world.
So there is a joke: some python hater said that "python" can only
crawl rather than run. :)

Python is slow because:
(1) dynamic binding
(2) it is a interpretation language
For example, in C code, an interger expression "a+b" will directly
generate an assembly code "add" for x86 processors.
A python interpreter, on the other side, need detect the type of a and
b first, then choose the right "+" operation for them and use a
evaluation stack to get the result.

Psyco is a JIT compiler with just in time specialization which can
somehow solve these two problems

More information about the Python-list mailing list