Python is darn fast (was: How fast is Python)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Hochberg

    #16
    Re: Python is darn fast (was: How fast is Python)

    Michele Simionato wrote:[color=blue]
    > I finally came to the conclusion that the exceeding good performance
    > of Psyco was due to the fact that the function was called a million
    > times with the *same* argument. Evidently Psyco is smart enough to
    > notice that. Changing the argument at each call
    > (erfc(0.456) -> i/1000000.0) slows down Python+Psyco at 1/4 of C speed.
    > Psyco improves Python performance by an order of magnitude, but still it
    > is not enough :-([/color]

    This is not suprising. Last I checked, Psyco does not fully compile
    floating point expressions. If, I rememeber correctly (though every time
    try to delve too deeply into Psyco my brains start oozing out my ears),
    there are three ways a in which a given chunk of code evaluated. At one
    level, which I'll call #1, Psyco generates the machine code(*) for the
    expression. At a second level, Psyco calls out to C helper functions,
    but still works with unboxed values. At the third level, Psyco punts and
    creates a Python object and hands things off to the interpreter.

    Most integer functions operate at level #1, so they tend to be quite
    fast. Most floating point operations operate at level #2, so they have a
    certain amount of overhead, but are still much faster than unpsyco
    (sane?) Python. I believe the reason for this is that x86 floating point
    operations are very messy, so Armin punted...

    (*) Armin is working on virtual machine implementation of Psyco, so it
    should be available on non x86 machines soon.

    FWIW,

    -tim

    [color=blue]
    > I was too optimistic!
    >
    > Here I my numbers for Python 2.3, Psyco 1.0, Red Hat Linux 7.3,
    > Pentium II 366 MHz:
    >
    > $ time p23 erf.py
    > real 0m3.245s
    > user 0m3.164s
    > sys 0m0.037s
    >
    > This is more than four times slower than optimized C:
    >
    > $ gcc erf.c -lm -O3
    > $ time ./a.out
    > real 0m0.742s
    > user 0m0.725s
    > sys 0m0.002s
    >
    > Here is the situation for pure Python
    >
    > $time p23 erf.jy
    > real 0m27.470s
    > user 0m27.162s
    > sys 0m0.023s
    >
    > and, just for fun, here is Jython performance:
    >
    > $ time jython erf.jy
    > real 0m44.395s
    > user 0m42.602s
    > sys 0m0.389s
    >
    > ----------------------------------------------------------------------
    >
    > $ cat erf.py
    > import math
    > import psyco
    > psyco.full()
    >
    > def erfc(x):
    > exp = math.exp
    >
    > p = 0.3275911
    > a1 = 0.254829592
    > a2 = -0.284496736
    > a3 = 1.421413741
    > a4 = -1.453152027
    > a5 = 1.061405429
    >
    > t = 1.0 / (1.0 + p*x)
    > erfcx = ( (a1 + (a2 + (a3 +
    > (a4 + a5*t)*t)*t)*t)* t ) * exp(-x*x)
    > return erfcx
    >
    > def main():
    > erg = 0.0
    >
    > for i in xrange(1000000) :
    > erg += erfc(i/1000000.0)
    >
    > if __name__ == '__main__':
    > main()
    >
    > --------------------------------------------------------------------------
    >
    > # python/jython version = same without "import psyco; psyco.full()"
    >
    > --------------------------------------------------------------------------
    >
    > $cat erf.c
    > #include <stdio.h>
    > #include <math.h>
    >
    > double erfc( double x )
    > {
    > double p, a1, a2, a3, a4, a5;
    > double t, erfcx;
    >
    > p = 0.3275911;
    > a1 = 0.254829592;
    > a2 = -0.284496736;
    > a3 = 1.421413741;
    > a4 = -1.453152027;
    > a5 = 1.061405429;
    >
    > t = 1.0 / (1.0 + p*x);
    > erfcx = ( (a1 + (a2 + (a3 +
    > (a4 + a5*t)*t)*t)*t)* t ) * exp(-x*x);
    >
    > return erfcx;
    > }
    >
    > int main()
    > {
    > double erg=0.0;
    > int i;
    >
    > for(i=0; i<1000000; i++)
    > {
    > erg = erg + erfc(i/1000000.0);
    > }
    >
    > return 0;
    > }
    >
    > Michele Simionato, Ph. D.
    > MicheleSimionat o@libero.it
    > http://www.phyast.pitt.edu/~micheles/
    > ---- Currently looking for a job ----[/color]

    Comment

    • dan

      #17
      Re: Python is darn fast (was: How fast is Python)

      mis6@pitt.edu (Michele Simionato) wrote in message
      news:<2259b0e2. 0308240638.3835 0cba@posting.go ogle.com>...
      [color=blue]
      > I finally came to the conclusion that the exceeding good performance
      > of Psyco was due to the fact that the function was called a million
      > times with the *same* argument. Evidently Psyco is smart enough to
      > notice that. Changing the argument at each call
      > (erfc(0.456) -> i/1000000.0) slows down Python+Psyco at 1/4 of C speed.
      > Psyco improves Python performance by an order of magnitude, but still it
      > is not enough :-(
      >[/color]
      It's plenty! A factor of 4 from optimized C, considering the newness
      and limited resources behind psyco, is very encouraging, and good
      enough for most tasks. Java JIT compilers are still around a factor
      of 2 slower than C, and they've had at least 2 orders of magnitude
      more whumpage.

      This is a far cry from the factor of 10-30 I've been seeing with pure
      python. For performance-critical code, this could be the difference
      between hand-coding 5% versus 20% of your code.

      Excellent news!!

      Comment

      • John J. Lee

        #18
        Re: Python is darn fast (was: How fast is Python)

        danbmil99@yahoo .com (dan) writes:
        [color=blue]
        > mis6@pitt.edu (Michele Simionato) wrote in message
        > news:<2259b0e2. 0308240638.3835 0cba@posting.go ogle.com>...[/color]
        [...][color=blue]
        > This is a far cry from the factor of 10-30 I've been seeing with pure
        > python. For performance-critical code, this could be the difference
        > between hand-coding 5% versus 20% of your code.
        >
        > Excellent news!![/color]

        If you care about this a lot, don't forget Pyrex.


        John

        Comment

        • dan

          #19
          Re: Python is darn fast (was: How fast is Python)

          right, pyrex -- looked at that a while ago. Compiled Python with
          C-style type declarations, right? Kinda like common lisp??? (I'm
          stretching my memory cells now)

          will review

          jjl@pobox.com (John J. Lee) wrote in message news:<87wud05e5 0.fsf@pobox.com >...[color=blue]
          > danbmil99@yahoo .com (dan) writes:
          >[color=green]
          > > mis6@pitt.edu (Michele Simionato) wrote in message
          > > news:<2259b0e2. 0308240638.3835 0cba@posting.go ogle.com>...[/color]
          > [...][color=green]
          > > This is a far cry from the factor of 10-30 I've been seeing with pure
          > > python. For performance-critical code, this could be the difference
          > > between hand-coding 5% versus 20% of your code.
          > >
          > > Excellent news!![/color]
          >
          > If you care about this a lot, don't forget Pyrex.
          >
          >
          > John[/color]

          Comment

          Working...