## Re: [Psyco-devel] Dynamic arrays, Pyrex and Psyco

 Re: [Psyco-devel] Dynamic arrays, Pyrex and Psyco From: Armin Rigo - 2003-06-10 11:06:32 ```Hello, On Mon, Jun 09, 2003 at 10:27:52PM +0000, sdfrost@... wrote: > As I imagine that lots of people would like to use Pyrex and/or Psyco t= o=20 > reduce the overhead of Python loops, I'm attaching a modified primes ex= ample,=20 > in which the size of the array of results is determined at runtime, and= =20 > compared the performance with standard Python code and Psyco-accelerera= ted=20 > code. Note that Psyco performs better if you write the loops in a more Pythonic= =20 style (which is an example of what I like to emphasis: given good tools i= t is=20 in the *interest* of performance to write higher-level code) : def primes(kmax): p =3D [] n =3D 2 while len(p) < kmax: for p1 in p: if n % p1 =3D=3D 0: break else: p.append(n) n =3D n + 1 =20 return p With Psyco, this only needs 80% of the time needed by your original examp= le. =20 Surely enough Pyrex is still faster. A bient=F4t, Armin. ```

 [Psyco-devel] Dynamic arrays, Pyrex and Psyco From: sdfrost@UCSD.EDU - 2003-06-09 22:29:35 Attachments: cprimes.pyx     run_cprimes.py ```Dear Pyrex and Psyco Lists, Thanks to Greg EDwing and Francesc Alted on the Pyrex list for pointing out the problem with my example of a dynamic array in Pyrex (I should read the docs more carefully!). As I imagine that lots of people would like to use Pyrex and/or Psyco to reduce the overhead of Python loops, I'm attaching a modified primes example, in which the size of the array of results is determined at runtime, and compared the performance with standard Python code and Psyco-accelererated code. The Pyrex code is about 20 times faster than the Python code, and about twice as fast as the Psyco-accelerated code. A good result for both approaches, I think. The setup.py script is: *** from distutils.core import setup from distutils.extension import Extension from Pyrex.Distutils import build_ext setup( name = 'Demos', ext_modules=[ Extension("cprimes", ["cprimes.pyx"]), ], cmdclass = {'build_ext': build_ext} ) *** Best wishes Simon ```
 Re: [Psyco-devel] Dynamic arrays, Pyrex and Psyco From: Armin Rigo - 2003-06-10 11:06:32 ```Hello, On Mon, Jun 09, 2003 at 10:27:52PM +0000, sdfrost@... wrote: > As I imagine that lots of people would like to use Pyrex and/or Psyco t= o=20 > reduce the overhead of Python loops, I'm attaching a modified primes ex= ample,=20 > in which the size of the array of results is determined at runtime, and= =20 > compared the performance with standard Python code and Psyco-accelerera= ted=20 > code. Note that Psyco performs better if you write the loops in a more Pythonic= =20 style (which is an example of what I like to emphasis: given good tools i= t is=20 in the *interest* of performance to write higher-level code) : def primes(kmax): p =3D [] n =3D 2 while len(p) < kmax: for p1 in p: if n % p1 =3D=3D 0: break else: p.append(n) n =3D n + 1 =20 return p With Psyco, this only needs 80% of the time needed by your original examp= le. =20 Surely enough Pyrex is still faster. A bient=F4t, Armin. ```
 Re: [Psyco-devel] Dynamic arrays, Pyrex and Psyco From: Tim Hochberg - 2003-06-10 14:58:40 ```Armin Rigo wrote: >Hello, > >On Mon, Jun 09, 2003 at 10:27:52PM +0000, sdfrost@... wrote: > > >>As I imagine that lots of people would like to use Pyrex and/or Psyco to >>reduce the overhead of Python loops, I'm attaching a modified primes example, >>in which the size of the array of results is determined at runtime, and >>compared the performance with standard Python code and Psyco-accelererated >>code. >> >> > >Note that Psyco performs better if you write the loops in a more Pythonic >style (which is an example of what I like to emphasis: given good tools it is >in the *interest* of performance to write higher-level code) : > > Oddly I don't see this speedup here: Windows XP using Psyco 1.0 on Python 2.2.2. The resulting code is easier to read but roughly the same speed or maybe just a tad slower. Have there been some speedups since 1.0 was put out? Perversely, where I do get a signifigant speed up is replacing "n % p1 == 0" with "(n // p1) * p1 == n". This speeds things up by almost 15%. I know that Python needs to jump through some hoops to make modulo work the Python way instead of the C way, and I assume that's what's slowing things down. If that really is the cause of the slowdown, would it be possible to recognize the "A % B == 0" case and use the raw C modulus there? I was going to try the latest version from CVS and see if it was faster and if there were any obvious bottlenecks involving modulus, but I can't get it to compile here. The compiler complains about not being able to find iencoding.h in vcompiler.h. Any hints for where I could find myself an iencoding.h file for cheap? -tim >def primes(kmax): > p = [] > n = 2 > while len(p) < kmax: > for p1 in p: > if n % p1 == 0: > break > else: > p.append(n) > n = n + 1 > return p > >With Psyco, this only needs 80% of the time needed by your original example. >Surely enough Pyrex is still faster. > > >A bientôt, > >Armin. > > >------------------------------------------------------- >This SF.net email is sponsored by: Etnus, makers of TotalView, The best >thread debugger on the planet. Designed with thread debugging features >you've never dreamed of, try TotalView 6 free at http://www.etnus.com. >_______________________________________________ >Psyco-devel mailing list >Psyco-devel@... >https://lists.sourceforge.net/lists/listinfo/psyco-devel > > > ```
 Re: [Psyco-devel] Dynamic arrays, Pyrex and Psyco From: Simon Frost - 2003-06-10 17:48:54 ```Dear Armin, I tried this implementation; however, I was surprised to note that while this style gave better performance in standard Python (about a 2-fold speed-up), it didn't give any improvement over the other style when the code was Python accelerated. Do you have any idea why this would be the case? Best Simon At 01:06 PM 6/10/2003 +0200, you wrote: >Note that Psyco performs better if you write the loops in a more Pythonic >style (which is an example of what I like to emphasis: given good tools it is >in the *interest* of performance to write higher-level code) : > >def primes(kmax): > p = [] > n = 2 > while len(p) < kmax: > for p1 in p: > if n % p1 == 0: > break > else: > p.append(n) > n = n + 1 > return p > >With Psyco, this only needs 80% of the time needed by your original example. >Surely enough Pyrex is still faster. Simon D.W. Frost, M.A., D.Phil. Department of Pathology University of California, San Diego Antiviral Research Center 150 W. Washington St., Suite 100 San Diego, CA 92103 USA Tel: +1 619 543 8080 x275 Fax: +1 619 298 0177 Email: sdfrost@... The information transmitted in this e-mail is intended only for the person(s) or entity to which it is addressed and may contain CONFIDENTIAL and/or privileged material. Any review, re-transmission, dissemination or other use of or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this e-mail in error, please contact the sender and delete/destroy the material/information from any computer. ```
 Re: [Psyco-devel] Dynamic arrays, Pyrex and Psyco From: Tim Hochberg - 2003-06-10 18:11:46 ```Armin Rigo wrote: >Hello, > >On Mon, Jun 09, 2003 at 10:27:52PM +0000, sdfrost@... wrote: > > >>As I imagine that lots of people would like to use Pyrex and/or Psyco to >>reduce the overhead of Python loops, I'm attaching a modified primes example, >>in which the size of the array of results is determined at runtime, and >>compared the performance with standard Python code and Psyco-accelererated >>code. >> >> > >Note that Psyco performs better if you write the loops in a more Pythonic >style (which is an example of what I like to emphasis: given good tools it is >in the *interest* of performance to write higher-level code) : > >def primes(kmax): > p = [] > n = 2 > while len(p) < kmax: > for p1 in p: > if n % p1 == 0: > break > else: > p.append(n) > n = n + 1 > return p > >With Psyco, this only needs 80% of the time needed by your original example. >Surely enough Pyrex is still faster. > > I looked into this a little and it turns out that Psyco currently does not psycoize integer modulo or division. I went ahead and added the code to do this(*) and immediately Armin's code above ran 22 times faster than the unpsycoized code. I don't have Pyrex handy, but based on the earler description, with this change they end up being about the same speed. -tim (*) The change is a little half baked -- it's implemented the same way the floating point operations are and as a result is probably not as fast as the other integer operations. This is because trying to mess with the stuff in the i386 folder makes my brain melt, so I just stuck with playing with pintobject. ```