Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: Python speed vs csharp

 
 
Richie Hindle
Guest
Posts: n/a
 
      07-31-2003

[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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Lulu of the Lotus-Eaters
Guest
Posts: n/a
 
      08-01-2003
Richie Hindle <(E-Mail Removed)> 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 <(E-Mail Removed)> ]--------------------------


 
Reply With Quote
 
 
 
 
Richie Hindle
Guest
Posts: n/a
 
      08-01-2003

[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
(E-Mail Removed)


 
Reply With Quote
 
Richie Hindle
Guest
Posts: n/a
 
      08-01-2003

> [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
(E-Mail Removed)


 
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 David M. Cooke Python 4 08-01-2003 04:51 AM
Re: Python speed vs csharp Martin v. =?iso-8859-15?q?L=F6wis?= Python 1 07-31-2003 10:37 PM



Advertisments