News for hackers. Stuff that matters.
Re:A reasonably speedy Python ray tracer by Twirrim on Sunday October 22, 2017 @01:41AM (#15525330)
Out of curiosity, I did a little digging around. I really need to take a step back and actually understand what is going on fully with the code, but a quick trot through cProfile showed a lot of effort hitting the dot method of vec3, primarily via abs(self) under the norm method.

There's a useful library for python called numexpr, http://numexpr.readthedocs.io/, which can speed up numpy operations, leveraging multiple cores etc. (and Intel's VML library if you have it installed), one I've been aware of but never got around to trying out.

At a quick stab, it seems like it can't take properties of classes? Either way, modifying dot a bit:

    def dot(self, other):
        self_x = self.x
        other_x = other.x
        self_y = self.y
        other_y = other.y
        self_z = self.z
        other_z = other.z
        return ne.evaluate("(self_x * other_x) + (self_y * other_y) + (self_z * other_z)")

at 400x400 this slows things down a little. Once you get above about 800x800 it starts to draw equal. By the time you get to 2000x2000 it's shaving some 10% of the execution time.

edit: here's a quick stab at using numexpr at just the most obvious places, without trying to consider major code refactoring. Note this bumps up the resolution to 2000x2000

https://gist.github.com/twirrim/64f523fd5e8be86eb392b90e9222...

compared to rt3 (at same resolution), I knock off over 10% on this 2015 retina mac:

  $ python rt3.py && python rt4.py
  Took 5.78933000565
  Took 4.9023668766

There are no comments on this comment yet.