Psyco testing feed-back wanted

Jimmy Retzlaff jimmy at
Fri Aug 23 23:52:53 CEST 2002


I've seen raves about Psyco, but I hadn't tried it myself until today.
My test case was a function that tests if a point is in a polygon. I
timed several runs with different approaches. All tests are whether 1
point is in a polygon made up of 5,000,000 points. The timing was done
on just the function call itself. Here are some results:

Python (no types declared, floats used) - 14.2 seconds (1x)
Python (no types declared, ints used) - 12.0 seconds (1.2x)
Pyrex (coordinates declared as object, floats used) - 4.8 seconds (3x)
Pyrex (coordinates declared as object, ints used) - 4.7 seconds (3x)
Pyrex (coordinates declared as float) - 2.3 seconds (6x)
Pyrex (coordinates declared as int) - 1.5 seconds (9.5x)
Psyco (no types declared, floats used) - 1.47 (9.5x)
C (coordinates declared as double) - 0.99 seconds (14x)
Psyco (no types declared, ints used) - 0.41 seconds (35x)
C (coordinates declared as unsigned int) - 0.20 seconds (71x)

It's amazing to get this type of code performing this close to C (in
this case VC6 optimized for speed) with no modifications and no type
declarations. For those of you who haven't tried Psycho, here is the
code that had to appear after my function definition:

import psyco

No other modifications were made to my Python code and Psyco requires no
modification to the Python installation, just drop the Psyco folder into
site-packages. The Python version of the test function is below.


def pointInPolygon((x, y), polygon):
  """Determine if the point (x, y) is in the polygon.

  >>> polygon = [(0, 0), (3, 3), (0, 6), (-3, 3)]
  >>> pointInPolygon((1, 2), polygon)
  >>> pointInPolygon((2, 1), polygon)

  # Algorithm is from
  # also appears at

  if len(polygon) < 3:
    return False

  result = False
  xOld, yOld = polygon[-1]
  for xNew, yNew in polygon:
    if xNew > xOld:
      if xOld < x <= xNew and (y-yOld)*(xNew-xOld) <
        result = not result
    elif xNew < x <= xOld and (y-yNew)*(xOld-xNew) <
      result = not result

    xOld = xNew
    yOld = yNew

  return result

More information about the Python-list mailing list