Recently, a post touting Numba was published. However, the example - a for loop - is unconvincing.
def monotonically_increasing(a): max_value = 0 for i in range(len(a)): if a[i] > max_value: max_value = a[i] a[i] = max_value
This converts an array such as
[1, 2, 1, 3, 3, 5, 4, 6]
[1, 2, 2, 3, 3, 5, 5, 6]
The code above will be hopelessly slow. To see what we're up against, consider the problem in R: since Python (like R) is dynamically typed,
< needs to pick an implementation with
every iteration. This overhead is avoided when using vector
operations instead of element-wise operations; in J the type is associated with an array and we need not extract an element, viz.
>. /\ 1 2 1 3 3 5 4 6
I'm not really sure what Numba does under the hood, nor what it would take to
perform as well as J/k. I don't really see the point though:
>. /\ and
|\ are far more concise
monotonically_increasing anyway. Numba does in fact allow
us to write for-loops instead of dropping down to a low-level language, but we
can get even better performance with clearer code via scans.
In general, one should avoid for loops when working with arrays.