Re: Python speed vs csharp

Discussion in 'Python' started by Richie Hindle, Jul 31, 2003.

  1. [Mike]
    > is there anything that can be done to get Python's speed close
    > to the speed of C#?


    Using Pyrex takes a million loops from 7.1 seconds to 1.3 seconds for me:

    ------------------------------- mike.pyx -------------------------------

    cdef extern from "math.h":
    double exp(double x)

    def erfc(x):
    cdef double f_x, p, a1, a2, a3, a4, a5, t

    f_x = x

    p = 0.3275911
    a1 = 0.254829592
    a2 = -0.284496736
    a3 = 1.421413741
    a4 = -1.453152027
    a5 = 1.061405429

    t = 1.0 / (1.0 + p*f_x)
    return ( (a1 + (a2 + (a3 + (a4 + a5*t)*t)*t)*t)*t ) * exp(-(f_x**2))


    ------------------------------- setup.py -------------------------------

    # Run with "python setup.py build"
    from distutils.core import setup
    from distutils.extension import Extension
    from Pyrex.Distutils import build_ext
    setup(
    name = "Mike",
    ext_modules=[
    Extension("mike", ["mike.pyx"])
    ],
    cmdclass = {'build_ext': build_ext}
    )


    ------------------------------- test.py -------------------------------

    # This takes about 1.3 seconds on my machine.
    import time
    from mike import erfc

    if __name__ == '__main__':
    start = time.time()
    for i in xrange(1000000):
    erfc(123.456)
    print time.time() - start


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

    Given that a million calls to "def x(): pass" take 0.9 seconds, the actual
    calculation time is about 0.4 seconds, down from 6.2. Not as fast as your
    C# results, but a lot better than you had before.

    Using Psyco dropped from 7.1 seconds to 5.4 - not great, but not bad for
    two lines of additional code:

    import psyco
    psyco.bind(erfc)

    Psyco currently only works on Intel-x86 compatible processors.

    Hope that helps,

    --
    Richie Hindle
     
    Richie Hindle, Jul 31, 2003
    #1
    1. Advertising

  2. Richie Hindle <> wrote previously:
    |Using Psyco dropped from 7.1 seconds to 5.4 - not great, but not bad for
    |two lines of additional code:
    | import psyco
    | psyco.bind(erfc)

    Try these two lines instead:

    import psyco
    psyco.full()

    I tried what Hindle did, intially (actually 'erfcp=psyco.proxy(erfc)'),
    and was disappointed by a similarly lackluster improvement (about the
    same order--he probably has a faster machine).

    Using the call to psyco.full() gives me a much nicer improvement from
    18 seconds -> 3.25 seconds.

    --
    ---[ to our friends at TLAs (spread the word) ]--------------------------
    Echelon North Korea Nazi cracking spy smuggle Columbia fissionable Stego
    White Water strategic Clinton Delta Force militia TEMPEST Libya Mossad
    ---[ Postmodern Enterprises <> ]--------------------------
     
    Lulu of the Lotus-Eaters, Aug 1, 2003
    #2
    1. Advertising

  3. [Richie]
    > Using Psyco dropped from 7.1 seconds to 5.4 - not great, but not bad for
    > two lines of additional code:
    > import psyco
    > psyco.bind(erfc)


    [Lulu]
    > Try these two lines instead:
    >
    > import psyco
    > psyco.full()
    >
    > 18 seconds -> 3.25 seconds.


    I tried that the first time but saw no improvement over bind... having
    checked the Psyco docs I see that it won't optimise code at module scope,
    and I had my driver loop at module scope. I now get 1.25 seconds down
    from 7.1 - slightly faster than Pyrex. Impressive.

    [OT: I hope Pyrex isn't suffering as a result of people writing little
    test scripts full of module-level code and being disappointed at the
    results.]

    --
    Richie Hindle
     
    Richie Hindle, Aug 1, 2003
    #3

  4. > [OT: I hope Pyrex isn't suffering as a result of people writing little
    > test scripts full of module-level code and being disappointed at the
    > results.]


    s/Pyrex/Psyco/g

    --
    Richie Hindle
     
    Richie Hindle, Aug 1, 2003
    #4
    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. Martin v. =?iso-8859-15?q?L=F6wis?=

    Re: Python speed vs csharp

    Martin v. =?iso-8859-15?q?L=F6wis?=, Jul 31, 2003, in forum: Python
    Replies:
    1
    Views:
    441
    Bengt Richter
    Jul 31, 2003
  2. David M. Cooke

    Re: Python speed vs csharp

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

    Re: Python speed vs csharp

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

    Re: Python speed vs csharp

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

    Re: Python speed vs csharp

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

Share This Page