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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michele Simionato

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

    I posted this few weeks ago (remember the C Sharp thread?) but it went
    unnoticed on the large mass of posts, so let me retry. Here I get Python+
    Psyco twice as fast as optimized C, so I would like to now if something
    is wrong on my old laptop and if anybody can reproduce my results.
    Here are I my numbers for calling the error function a million times
    (Python 2.3, Psyco 1.0, Red Hat Linux 7.3, Pentium II 366 MHz):

    $ time p23 erf.py
    real 0m0.614s
    user 0m0.551s
    sys 0m0.029s

    This is twice as fast as optimized C:

    $ gcc erf.c -lm -o3
    $ time ./a.out
    real 0m1.125s
    user 0m1.086s
    sys 0m0.006s

    Here is the situation for pure Python

    $time p23 erf.jy
    real 0m25.761s
    user 0m25.012s
    sys 0m0.049s

    and, just for fun, here is Jython performance:

    $ time jython erf.jy
    real 0m42.979s
    user 0m41.430s
    sys 0m0.361s

    The source code follows (copied from Alex Martelli's post):

    ----------------------------------------------------------------------

    $ 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(0.456)

    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(0.456);
    }

    return 0;
    }

    Michele Simionato, Ph. D.
    MicheleSimionat o@libero.it

    --- Currently looking for a job ---
  • Irmen de Jong

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

    Michele Simionato wrote:[color=blue]
    > I posted this few weeks ago (remember the C Sharp thread?) but it went
    > unnoticed on the large mass of posts, so let me retry. Here I get Python+
    > Psyco twice as fast as optimized C, so I would like to now if something
    > is wrong on my old laptop and if anybody can reproduce my results.[/color]

    I can. :-)

    I had to increase the loop counter by a factor of 10 because it
    ran too fast on my machine (celeron 533 mhz), and added a print statement
    of the accumulated sum (erg). These are my results:

    [irmen@atlantis]$ gcc -O3 -march=pentium2 -mcpu=pentium2 -lm erf.c

    [irmen@atlantis]$ time ./a.out
    5190039.338694
    4.11user 0.00system 0:04.11elapsed 99%CPU (0avgtext+0avgd ata 0maxresident)k
    0inputs+0output s (103major+13min or)pagefaults 0swaps

    [irmen@atlantis]$ time python2.3 erf.py
    5190039.33869
    2.91user 0.01system 0:02.92elapsed 99%CPU (0avgtext+0avgd ata 0maxresident)k
    0inputs+0output s (544major+380mi nor)pagefaults 0swaps

    This is with gcc 3.2.2 on Mandrake 9.1.

    While Python + Psyco is not twice as fast as compiled & optimized C,
    it's still faster by almost 30% on my system, which is still great!!

    --Irmen

    Comment

    • Irmen de Jong

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

      Michele Simionato wrote:[color=blue]
      > I posted this few weeks ago (remember the C Sharp thread?) but it went
      > unnoticed on the large mass of posts, so let me retry. Here I get Python+
      > Psyco twice as fast as optimized C, so I would like to now if something
      > is wrong on my old laptop and if anybody can reproduce my results.[/color]

      I can. :-)

      I had to increase the loop counter by a factor of 10 because it
      ran too fast on my machine (celeron 533 mhz), and added a print statement
      of the accumulated sum (erg). These are my results:

      [irmen@atlantis]$ gcc -O3 -march=pentium2 -mcpu=pentium2 -lm erf.c

      [irmen@atlantis]$ time ./a.out
      5190039.338694
      4.11user 0.00system 0:04.11elapsed 99%CPU (0avgtext+0avgd ata 0maxresident)k
      0inputs+0output s (103major+13min or)pagefaults 0swaps

      [irmen@atlantis]$ time python2.3 erf.py
      5190039.33869
      2.91user 0.01system 0:02.92elapsed 99%CPU (0avgtext+0avgd ata 0maxresident)k
      0inputs+0output s (544major+380mi nor)pagefaults 0swaps

      This is with gcc 3.2.2 on Mandrake 9.1.

      While Python + Psyco is not twice as fast as compiled & optimized C,
      it's still faster by almost 30% on my system, which is still great!!

      --Irmen

      Comment

      • Lawrence Oluyede

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

        Michele Simionato wrote:
        [color=blue]
        > $ time p23 erf.py
        > real 0m0.614s
        > user 0m0.551s
        > sys 0m0.029s
        >
        > This is twice as fast as optimized C:
        >
        > $ gcc erf.c -lm -o3
        > $ time ./a.out
        > real 0m1.125s
        > user 0m1.086s
        > sys 0m0.006s
        >
        > Here is the situation for pure Python
        >
        > $time p23 erf.jy
        > real 0m25.761s
        > user 0m25.012s
        > sys 0m0.049s
        >
        > and, just for fun, here is Jython performance:
        >
        > $ time jython erf.jy
        > real 0m42.979s
        > user 0m41.430s
        > sys 0m0.361s[/color]

        Mmm...on my machine C is faster. What version of GCC do you have? I think
        2.9x, right?

        These are my timings (Debian GNU Linux Unstable, Duron 1300, Python2.3,
        Psyco CVS, GCC 3.3.2, Java 1.4.1):

        $ time python erf.py

        real 0m0.251s
        user 0m0.207s
        sys 0m0.012s

        $ gcc erf.c -lm -O3
        $ time ./a.out

        real 0m0.162s
        user 0m0.157s
        sys 0m0.001s

        Notice that C is faster than Psyco + Python2.3 on my machine (about 65% of
        speedup)

        Without Psyco Python2.3 tooks about 6 seconds

        $ time python erf.jy

        real 0m6.177s
        user 0m6.040s
        sys 0m0.010s


        And Jython is definitely slower :)

        $ time jython erf.jy

        real 0m10.423s
        user 0m9.506s
        sys 0m0.197s


        --
        Lawrence "Rhymes" Oluyede
        Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

        rhymes@NOSPAMmy self.com

        Comment

        • Lawrence Oluyede

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

          Michele Simionato wrote:
          [color=blue]
          > $ time p23 erf.py
          > real 0m0.614s
          > user 0m0.551s
          > sys 0m0.029s
          >
          > This is twice as fast as optimized C:
          >
          > $ gcc erf.c -lm -o3
          > $ time ./a.out
          > real 0m1.125s
          > user 0m1.086s
          > sys 0m0.006s
          >
          > Here is the situation for pure Python
          >
          > $time p23 erf.jy
          > real 0m25.761s
          > user 0m25.012s
          > sys 0m0.049s
          >
          > and, just for fun, here is Jython performance:
          >
          > $ time jython erf.jy
          > real 0m42.979s
          > user 0m41.430s
          > sys 0m0.361s[/color]

          Mmm...on my machine C is faster. What version of GCC do you have? I think
          2.9x, right?

          These are my timings (Debian GNU Linux Unstable, Duron 1300, Python2.3,
          Psyco CVS, GCC 3.3.2, Java 1.4.1):

          $ time python erf.py

          real 0m0.251s
          user 0m0.207s
          sys 0m0.012s

          $ gcc erf.c -lm -O3
          $ time ./a.out

          real 0m0.162s
          user 0m0.157s
          sys 0m0.001s

          Notice that C is faster than Psyco + Python2.3 on my machine (about 65% of
          speedup)

          Without Psyco Python2.3 tooks about 6 seconds

          $ time python erf.jy

          real 0m6.177s
          user 0m6.040s
          sys 0m0.010s


          And Jython is definitely slower :)

          $ time jython erf.jy

          real 0m10.423s
          user 0m9.506s
          sys 0m0.197s


          --
          Lawrence "Rhymes" Oluyede
          Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

          rhymes@NOSPAMmy self.com

          Comment

          • P@draigBrady.com

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

            Michele Simionato wrote:[color=blue]
            > I posted this few weeks ago (remember the C Sharp thread?) but it went
            > unnoticed on the large mass of posts, so let me retry. Here I get Python+
            > Psyco twice as fast as optimized C
            >
            > $ gcc erf.c -lm -O3[/color]

            try a 3.x series gcc with the appropriate -march=pentium3
            You'll be pleasently surprised. I can't understand how
            the sudden improvment of gcc code generation lately hasn't
            been hyped more? If you want to try different machines
            then http://www.pixelbeat.org/scripts/gcccpuopt will give
            you the appropriate machine specific gcc options to use.
            Note also -ffast-math might help a lot in this application?

            cheers,
            Pádraig.

            Comment

            • P@draigBrady.com

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

              Michele Simionato wrote:[color=blue]
              > I posted this few weeks ago (remember the C Sharp thread?) but it went
              > unnoticed on the large mass of posts, so let me retry. Here I get Python+
              > Psyco twice as fast as optimized C
              >
              > $ gcc erf.c -lm -O3[/color]

              try a 3.x series gcc with the appropriate -march=pentium3
              You'll be pleasently surprised. I can't understand how
              the sudden improvment of gcc code generation lately hasn't
              been hyped more? If you want to try different machines
              then http://www.pixelbeat.org/scripts/gcccpuopt will give
              you the appropriate machine specific gcc options to use.
              Note also -ffast-math might help a lot in this application?

              cheers,
              Pádraig.

              Comment

              • John J. Lee

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

                Irmen de Jong <irmen@-NOSPAM-REMOVETHIS-xs4all.nl> writes:
                [color=blue]
                > P@draigBrady.co m wrote:
                >[color=green]
                > > try a 3.x series gcc with the appropriate -march=pentium3
                > > You'll be pleasently surprised.[/color]
                >
                > In my other reply I mentioned that I still get a Python+Psyco
                > advantage of 30% over a gcc 3.2.2 compiled version.
                > My gcc is doing a lot better than Michele's reported 50% difference,
                > but Python+Psyco still wins :-)[/color]

                So, the interesting part is: why?


                John

                Comment

                • John J. Lee

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

                  Irmen de Jong <irmen@-NOSPAM-REMOVETHIS-xs4all.nl> writes:
                  [color=blue]
                  > P@draigBrady.co m wrote:
                  >[color=green]
                  > > try a 3.x series gcc with the appropriate -march=pentium3
                  > > You'll be pleasently surprised.[/color]
                  >
                  > In my other reply I mentioned that I still get a Python+Psyco
                  > advantage of 30% over a gcc 3.2.2 compiled version.
                  > My gcc is doing a lot better than Michele's reported 50% difference,
                  > but Python+Psyco still wins :-)[/color]

                  So, the interesting part is: why?


                  John

                  Comment

                  • Simon Burton

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

                    On Sun, 24 Aug 2003 00:31:15 +0100, John J. Lee wrote:
                    [color=blue]
                    > Irmen de Jong <irmen@-NOSPAM-REMOVETHIS-xs4all.nl> writes:
                    >[color=green]
                    >> P@draigBrady.co m wrote:
                    >>[/color][/color]
                    ....[color=blue][color=green]
                    >> but Python+Psyco still wins :-)[/color]
                    >
                    > So, the interesting part is: why?
                    >
                    >
                    > John[/color]

                    My suspicion is that when psyco looks at erfc, it
                    finds that nothing changes and so replaces the
                    function call with the resulting number (am i right? it's the
                    same each time?). This is what a "specializi ng compiler"
                    would do, me thinks. So, try using a different number
                    with each call.

                    Simon.

                    Comment

                    • Simon Burton

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

                      On Sun, 24 Aug 2003 00:31:15 +0100, John J. Lee wrote:
                      [color=blue]
                      > Irmen de Jong <irmen@-NOSPAM-REMOVETHIS-xs4all.nl> writes:
                      >[color=green]
                      >> P@draigBrady.co m wrote:
                      >>[/color][/color]
                      ....[color=blue][color=green]
                      >> but Python+Psyco still wins :-)[/color]
                      >
                      > So, the interesting part is: why?
                      >
                      >
                      > John[/color]

                      My suspicion is that when psyco looks at erfc, it
                      finds that nothing changes and so replaces the
                      function call with the resulting number (am i right? it's the
                      same each time?). This is what a "specializi ng compiler"
                      would do, me thinks. So, try using a different number
                      with each call.

                      Simon.

                      Comment

                      • Van Gale

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

                        Lawrence Oluyede wrote:[color=blue]
                        > P@draigBrady.co m wrote:
                        >[color=green]
                        >>If you want to try different machines
                        >>then http://www.pixelbeat.org/scripts/gcccpuopt will give
                        >>you the appropriate machine specific gcc options to use.[/color]
                        >
                        > Very cool script, thanks :) Anyway it didn't change so much with erf.c
                        > erfCPU is compiled with the flags suggested by gcccpuopt script:
                        >
                        > $ gcccpuopt
                        > -march=athlon-xp -mfpmath=sse -msse -mmmx -m3dnow[/color]

                        You still need some -O optimization flags. The -m options just let gcc
                        generate some nice instructions specific to your Athlon CPU.

                        Also, I don't think that script is all that useful because at least some
                        (if not all) of those -m options are already implied by -march=athlon-xp
                        (I don't recall which ones off the top of my head but I'll find a
                        reference for anyone interested... you can also find out by looking at
                        the gcc command line option parsing code).

                        Anyone who wants some other good ideas for the best flags on their
                        machine check out ccbench:



                        The problem here of course is that not all applications behave like the
                        benchmarks :(

                        Van Gale

                        Comment

                        • Lawrence Oluyede

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

                          Van Gale wrote:
                          [color=blue]
                          > You still need some -O optimization flags. The -m options just let gcc
                          > generate some nice instructions specific to your Athlon CPU.[/color]

                          I didn't mention but I also used -O3 flag. I don't know why but on my
                          machine C code is faster than psyco code in this test

                          --
                          Lawrence "Rhymes" Oluyede
                          Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

                          rhymes@NOSPAMmy self.com

                          Comment

                          • Michele Simionato

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

                            Van Gale <news@exultants .org> wrote in message news:<XKW1b.451 2$TE6.1447@news svr27.news.prod igy.com>...[color=blue]
                            > Michele Simionato wrote:[color=green]
                            > > I posted this few weeks ago (remember the C Sharp thread?) but it went
                            > > unnoticed on the large mass of posts, so let me retry. Here I get Python+
                            > > Psyco twice as fast as optimized C, so I would like to now if something
                            > > is wrong on my old laptop and if anybody can reproduce my results.
                            > > Here are I my numbers for calling the error function a million times
                            > > (Python 2.3, Psyco 1.0, Red Hat Linux 7.3, Pentium II 366 MHz):
                            > >
                            > > $ gcc erf.c -lm -o3[/color]
                            >
                            > Did you really use "-o3" instead of "-O3"? The lowercase -o3 will
                            > produce object code file named "3" instead of doing optimization.[/color]

                            Yes, I used -O3, this was a misprint in the e-email. The compiler was
                            gcc 2.96.

                            Michele Simionato, Ph. D.
                            MicheleSimionat o@libero.it

                            --- Currently looking for a job ---

                            Comment

                            • Michele Simionato

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

                              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 :-(

                              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

                              ---- Currently looking for a job ----

                              Comment

                              Working...