Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Your general point is correct, although in this specific instance, replacing the loop body with

    z **= 2 
    z +=c
gets rid of the temps. But yes there are cases where that isn't possible.


This gets rid of temporary arrays, but this still isn't optimal if z is large. Memory locality means it's faster to apply a scalar operation like z2+c in a single pass, rather than in two separate passes.

Explicitly unrolling loopy code (e.g., in pypy or Numba) is one easy way to achieve this, but you have to write more code.

Julia has some really nice syntax that lets you write things in this clean vectorized way but still get efficient code: https://julialang.org/blog/2017/01/moredots/


Gotcha. I feel like I remember some numpy or scipy way of creating complex ufunc ops and applying them simultaneously, but maybe I'm misremembering or thinking np.vectorize was fast?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: