Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Python speed vs csharp

Reply
Thread Tools

Re: Python speed vs csharp

 
 
Martin v. =?iso-8859-15?q?L=F6wis?=
Guest
Posts: n/a
 
      07-31-2003
Mike <> 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

 
Reply With Quote
 
 
 
 
Bengt Richter
Guest
Posts: n/a
 
      07-31-2003
On 31 Jul 2003 08:29:26 +0200, (Martin v. =?iso-8859-15?q?L=F6wis?=) wrote:

>Mike <> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Python speed vs csharp Greg Brunet Python 13 08-04-2003 07:06 PM
Re: Python speed vs csharp Mike Python 2 08-03-2003 03:09 PM
Re: Python speed vs csharp Bengt Richter Python 1 08-02-2003 02:08 AM
Re: Python speed vs csharp Richie Hindle Python 3 08-01-2003 12:20 PM
Re: Python speed vs csharp David M. Cooke Python 4 08-01-2003 04:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57