Hi Juan,
I don't know how to get a 10x speedup on this function, but I tinkered
with it for a bit, so I'll report my findings.
- A pure numpy solution is ~3.8x faster than your posted numba code,
at least for the small example image you suggested. (But it is much
less straightforward to read.)
- If I eliminate zip() from your numba solution, I see a ~5.2x speedup.
- I tried computing 'cr' with np.power() instead of an explicit loop,
but that made it ~3x SLOWER.
- Just out of curiosity, I tried inverting the nesting order of the
outer two loops. As expected, that slowed things down, just slightly.
So, the best optimization I could find here is to avoid zip(), but
that still doesn't achieve cython speeds.
Best,
Stuart
PS -- For reference, my (barely-tested) numpy implementation is below:
import numpy as np
def moments_central_numpy(image, center=None, order=3):
mu = np.zeros((order + 1,) * image.ndim)
if center is None:
center = np.mean(np.column_stack(np.nonzero(image)), axis=0)
# pre-compute centralized coordinates along each axis
offsets = []
for d in range(image.ndim):
axis_offset = np.arange(image.shape[d]) - center[d]
axis_offset.shape = (1,)*d + (image.shape[d],) + (1,)*(image.ndim-d-1)
offsets.append( axis_offset )
# pre-compute (c - ctr)^p for each axis and each p
offset_powers = {}
for d in range(image.ndim):
for p in range(mu.shape[d]):
offset_powers[(d,p)] = np.power(offsets[d], p)
prods = np.zeros_like(image)
for powers in np.ndindex(mu.shape):
prods[:] = image
for d, p in enumerate(powers):
prods[:] *= offset_powers[(d,p)]
mu[powers] = prods.sum()
return mu
> --
> You received this message because you are subscribed to the Google Groups
> "Numba Public Discussion - Public" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
numba-users...@continuum.io.
> To post to this group, send email to
numba...@continuum.io.
> To view this discussion on the web visit
>
https://groups.google.com/a/continuum.io/d/msgid/numba-users/d6acd47e-6406-4112-8ad1-c8f380f154d2%40Spark.
> For more options, visit
https://groups.google.com/a/continuum.io/d/optout.