Python speed vs csharp

Martin v. Löwis martin at v.loewis.de
Thu Jul 31 02:29:26 EDT 2003


Mike <mike at nospam.com> writes:

> My first question is, why is the Python code, at 210 seconds, so much
> slower?

One would have to perform profiling, but in this case, it is likely
because numbers are objects in Python, so each multiplication results
in a memory allocation. In addition, as soon as the parameters of the
multiplication are not needed anymore, you get a deallocation of the
temporaries.

There is also an overhead for the byte code interpretation, but this
is likely less significant.

> My second question is, is there anything that can be done to get Python's
> speed close to the speed of C#?

C# implementations typically do just-in-time compilation to machine
code. They represent numbers as primitive (machine) values, directly
using machine operations for the multiplication. Doing the same for
Python is tricky.

I recommend that you try out Python 2.3. It has significantly improved
memory allocation mechanisms, so you should see some speed-up.

You could also try Psyco, which is a just-in-time compiler for
Python. It should give very good results in this case, also.

Regards,
Martin





More information about the Python-list mailing list