jdf/processing.py — pull request #44 by kazimuth (2014-04-20 02:59:30+00:00)
Title: Add & speed up PVector operations, clean up namespace

Another pull request!
This does a few nice semantic things for PVector:
* Adds an ```__rmul__``` method for PVector, so that you can say ```5 * PVector(2, 3, 4)``` without receiving a TypeError.
* Adds a ```__div__``` method for PVector so that they can be divided by scalars (as a shorthand for ```(1/a) * v```).

It also does a few performance things:
* Uses native java methods for PVector operations instead of python methods.
* Specializes incremental operators (```__iadd__``` for ```+=``` and so on) for PVector so that they update the vector in-place instead of allocating new copies.

Normally, I wouldn't worry about microoptimizing like this, but vectors are the sort of things that could be modified in hot loops, and it's a simple change. According to a completely unscientific benchmark I rigged up (lots of vector operations with ```timeit()```), using Java code for ```*``` makes it twice as fast, and destructively updating a variable with ```*=``` is four times as fast.

Finally, this cleans up the functions after they're patched onto PVector, since they were still hanging out in the global namespace.