Optimization Posts

2 posts tagged with optimization

I recently I had the opportunity to speed up some Websocket code that was a major bottleneck. The final solution was 60X (!) faster than the first pass, and an interesting exercise in optimizing inner loops.

When you send data via a websocket it is masked with a randomly generated four byte key. This masking provides protection from certain exploits against proxies (details here). The algorithm is super simple; essentially each byte in the payload is XORed with a corresponding byte from the key.

The first version of my XOR mask function looked this:

That's the kind of unfussy and elegant code you want to write for a job interview. If that's the solution that first came to mind, then you know your Python. Alas, this code is as slow as it is beautiful. continue reading…

Today I discovered a rather cool project via Reddit. MiniLight is a “minimal global illumination renderer” (it draws 3D scenes) with implementations in various languages.

The Cornell Box, rendered with Minilight

The Python version is a lot slower than the compiled languages – which is to be expected, number crunching like this is not Python's forte. All the same, I had a go at optimizing it. Using similar techniques I wrote about in a previous blog post I reduced the run-time for the test scene from 61.4 seconds to 53.2 seconds. Hardly stellar, and it's not going to change the comparisons, but it was an interesting exercise. continue reading…