Lazy evaluation: overloading the assignment operator?

sturlamolden sturlamolden at yahoo.no
Thu May 3 05:39:44 EDT 2007


On May 3, 6:22 am, Charles Sanders <C.delete_this.Sand... at BoM.GOv.AU>
wrote:

>         y = a*b+c*d   # Returns a proxy object
>
>         x = y[4]      # Computes x = a[4]*b[4] + c[4]*d[4]
>
>         v = y.eval()  # Evaluates all elements, returning Xarray
>
>         z = ((a+b)*(c+d)).eval()  # Also evaluates all elements

When I suggested this on the NumPy mailing list, I too suggested using
the indexing operator to trigger the computations. But I am worried
that if an expression like

y = a*b+c*d

returns a proxy, it is somehow possible to mess things up by creating
cyclically dependent proxies. I may be wrong about this, in which case
__getitem__ et al. will do the job.


> Whether it would be any faster is doubtful, but it would eliminate
> the temporaries.

The Numexpr compiler in SciPy suggests that it can. It parses an
expression like 'y = a*b+c*d' and evaluates it. Numexpr  is only a
slow prototype written in pure Python, but still it can sometimes give
dramatical speed-ups. Here we do not even need all the machinery of
Numexpr, as Python creates the parse tree on the fly.

Inefficiency of binary operators that return temporary arrays is
mainly an issue when the arrays in the expression is too large to fit
in cache. RAM access can be very expensive, but cache access is
usually quite cheap. One also avoids unnecessary allocation and
deallocation of buffers to hold temporary arrays. Again, it is mainly
an issue when arrays are large, as malloc and free can be rather
efficient for small objects.









More information about the Python-list mailing list