Python: is X is better than Y ? Round 3, pure python VS cython

Round 3: pure python drawing VS cython

Benchmark code :

Let’s take the Python version of simplified drawRectangle():

I got the result on poor graphics card: average ~1.1268s

Let’s rewrite in Cython:

And the result : average ~0.0325s

PyMT Impact: rewriting graphx package 🙂

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…

Python: is X is better than Y ? Round 2, remove exception VS test in + remove

Round 2: catching remove exception VS test in + remove

Benchmark code :

And the result :

We can see a little improvement with testing exception instead of testing in.
We can also see the overhead due to the test before removing in list.

I’ve also tested with xrange(-500, 500) instead of (0, 500), to trigger invalid removal. Here is the result :

Triggering an exception cost much time than testing if value is in a list…

PyMT Impact: must check.

Python: is X is better than Y ? Round 1, deque vs list.

This week-end, i’ve spend some time about searching how to optimize PyMT code. And done some interesting benchmark. Next days, i’ll post some of them, in order to remember which is the better solution.

For the first round: let’s test Deque from collections package VS Python List !


And the result :

Impact for PyMT: wm_pen, wm_touch.

Python and HTTP Pipelining

Attention, this method is NOT pipelining as described in comments, and even might break if the http connection is too fast with httplib.ResponseNotReady. I’ll update this post when i’ll found a real and simple way to achieve pipelining, because one possible way to do it with httplib is really ugly

I wanted to do HTTP Pipelining using urllib2. But, first of all, what is pipelining ?
HTTP pipelining is a technique in which multiple HTTP requests are written out to a single socket without waiting for the corresponding responses.

What is the benefit of pipelining ? Less network load, speedup processing !
I was searching a way to do it with urllib2… But solution are complicated, and not fit well to my needs.
But way, why stay on urllib2 ? Use httplib !

Reusing the connection :

First, reusing the same connection

Second, try pipelining !

Color swapping and Python

For PyMT, Sharath need BGR support into PyMT, while his graphic card don’t support GL_BGR.
Well, after adapting swap code from Pyglet sourcecode, he say: “< patali> it works but very slow”

The goal is to swap a string from ‘bgrbgrbgr’ to ‘rgbrgbrgb’. Just let’s do a rapid benchmark.

Swap1() is actually the way of pyglet. swap2/swap3 is from me, and others is found on some threads in pygame mailing list.

So, who win ?

The array solution is a lot faster than any other implementation ! And it’s python standard 🙂 Let’s take this…

Edit: thanks for Milan to come with another awesome solution !

Result is: 0.0059 !