[Pyrex] Dynamic arrays, Pyrex and Psyco

sdfrost at UCSD.EDU sdfrost at UCSD.EDU
Tue Jun 10 00:27:52 CEST 2003


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cprimes.pyx
Type: application/octet-stream
Size: 460 bytes
Desc: not available
Url : http://lists.copyleft.no/pipermail/pyrex/attachments/20030609/91ba6dac/cprimes.obj
-------------- next part --------------
from cprimes import cprimes
import time

def primes(kmax):
  p = []
  k = 0
  n = 2
  while k < kmax:
    i = 0
    while i < k and n % p[i] <> 0:
      i = i + 1
    if i == k:
      p.append(n)
      k = k + 1
    n = n + 1
  return p

if __name__=="__main__":
  t1=time.time()
  cprimes(10000)
  print time.time()-t1
  t2=time.time()
  primes(10000)
  print time.time()-t2
  try:
    import psyco
    psyco.full()
    t3=time.time()
    primes(10000)
    print time.time()-t3
  except ImportError:
    pass



More information about the Pyrex mailing list