Round 3: pure python drawing VS cython
Benchmark code :
s = time.time()
for x in xrange(10000):
drawRectangle(0, 0, 50, 50)
print 'result=', time.time() - s
Let’s take the Python version of simplified drawRectangle():
def drawRectangle(x, y, w, h):
glBegin(GL_QUADS)
glVertex2f(x, y)
glVertex2f(x + w, y)
glVertex2f(x + w, y + h)
glVertex2f(x, y + h)
glEnd()
I got the result on poor graphics card: average ~1.1268s
Let’s rewrite in Cython:
cdef extern from "GL/gl.h":
ctypedef float GLfloat
ctypedef unsigned int GLenum
int GL_QUADS
cdef void glBegin(GLenum mode)
cdef void glEnd()
cdef void glVertex2f(GLfloat x, GLfloat y)
def drawRectangle(float x, float y, float w, float h):
glBegin(GL_QUADS)
glVertex2f(x, y)
glVertex2f(x + w, y)
glVertex2f(x + w, y + h)
glVertex2f(x, y + h)
glEnd()
And the result : average ~0.0325s
PyMT Impact: rewriting graphx package 🙂
EDIT:
On a NVIDIA 9800 GT: with 1000000 (instead of 10000):
- Python: 16.1
- Python -O: 17.2
- Python -OO: 16.9
- Cython: 0.29
Very weird about -O / -OO…