[Pyrex] C versus Python access

David McNab david at rebirthing.co.nz
Tue Oct 30 23:09:28 CET 2007


Hi folks,

I'm trying to optimise a pile of Pyrex code, mainly by using C method
calls wherever possible - but trying to keep the more important methods
accessible from Python.

To give a simple and very silly example:

        cdef class Rect:
        
            cdef readonly int width, height
        
            def __init__(self, int width, int height):
                self.width = width
                self.height = height
        
            cdef public int _area(self):
                return self.width * self.height
        
            def area(self):
                return self._area()

Here, I've defined a C method '_area', which can be called efficiently
from Pyrex code without all the argument parsing, getattr, python object
calls etc. Also, Python method 'area' is a thin wrapper around '_area()'
which makes '_area()' available to Python callers.

In this approach, I'm trying to get the best of both worlds - methods
which can be called efficiently at C level, but are also wrapped and
exposed to Python callers.

Is there a better way to shave off CPU cycles?

Cheers
David






More information about the Pyrex mailing list