Python speed vs csharp

Bengt Richter bokr at oz.net
Fri Aug 1 00:37:25 CEST 2003


On 31 Jul 2003 08:29:26 +0200, martin at v.loewis.de (Martin v. =?iso-8859-15?q?L=F6wis?=) wrote:

>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.
>
I just made a C extension out erfc and only got about 5-6 times improvement over the
python version, which makes me think that most of the time is in call setup and passing
parameters, which as you say involves memory allocation/deallocation.

I don't see that csharp could improve on that. The benefit would really only show up
fullfledged if the calling loop is using C calling methods, as in numeric array processing
that only makes one call from python and many in C, depending on arrary dimensions.

I wonder what it would take to have the Python VM do primary allocation of ints and doubles
on the stack C-style and only migrate them to heap if/when exported. Then you could
bypass exporting when calling a trusted C extension, and in a case like erfc
you could just make the raw C call and replace the value on top of the stack, and go on
from there until you hit an export trigger.

Regards,
Bengt Richter




More information about the Python-list mailing list