Re: Python speed vs csharp

Discussion in 'Python' started by Martin v. =?iso-8859-15?q?L=F6wis?=, Jul 31, 2003.

  1. 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
     
    Martin v. =?iso-8859-15?q?L=F6wis?=, Jul 31, 2003
    #1
    1. Advertising

  2. 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
     
    Bengt Richter, Jul 31, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richie Hindle

    Re: Python speed vs csharp

    Richie Hindle, Jul 31, 2003, in forum: Python
    Replies:
    3
    Views:
    448
    Richie Hindle
    Aug 1, 2003
  2. David M. Cooke

    Re: Python speed vs csharp

    David M. Cooke, Jul 31, 2003, in forum: Python
    Replies:
    4
    Views:
    471
    Andrew Dalke
    Aug 1, 2003
  3. Bengt Richter

    Re: Python speed vs csharp

    Bengt Richter, Aug 1, 2003, in forum: Python
    Replies:
    1
    Views:
    354
    John Machin
    Aug 2, 2003
  4. Greg Brunet

    Re: Python speed vs csharp

    Greg Brunet, Aug 1, 2003, in forum: Python
    Replies:
    13
    Views:
    610
    Michele Simionato
    Aug 4, 2003
  5. Mike

    Re: Python speed vs csharp

    Mike, Aug 2, 2003, in forum: Python
    Replies:
    2
    Views:
    321
Loading...

Share This Page