4 ms·
I wrote a pure python (No Numpy) ray tracer as a learning exercise. Spoiler: it was slowwwwwwwwwww. I converted to NumPy and it was just slow. I then went to a
by Marazan 9y ago
I wrote a pure python (No Numpy) ray tracer as a learning exercise. Spoiler: it was slowwwwwwwwwww.
I converted to NumPy and it was just slow. I then went to array broadcasting (which required surprisingly few code changes due to NumPy being pretty awesome) and it became fast.
- tgb 9y agoI'm familiar with the concept of broadcasting in Numpy, but I don't understand what it means in this context. Can someone explain?
- __s 9y agoBasically map https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html https://docs.scipy.org/doc/numpy/user/basics.broadcasting.ht... So OP would've converted from indexing numpy arrays in Python space
- Marazan 9y ago__s basically covers it but my initial switch from pure python to NumPy just involved changing my vector 'class' (just a tuple in reality) into NumPy arrays. Whilst the basic vector multiplications and additions became way faster the overhead of creating hundreds of tiny NumPy arrays was a killer. So, just like in the original article instead of creating a single 1x3 array I created a Mx3 array where M represented as many rays as I could fit into memory at once (I have quite a weedy machine). Due to how NumPy broadcasting exactly the same code for, say, subtracting the origin from a ray vector works to subtract that single origin vector for a multidimensional array of ray vectors.
- tgb 9y agoI see now, thanks.