[Numpy-discussion] Silent Broadcasting considered harmful
Sturla Molden
sturla.molden at gmail.com
Mon Feb 9 02:29:42 EST 2015
On 08/02/15 23:17, Stefan Reiterer wrote:
> Actually I use numpy for several years now, and I love it.
> The reason that I think silent broadcasting of sums is bad
> comes simply from the fact, that I had more trouble with it, than it
> helped me.
In Fortran 90, broadcasting allows us to write concise expressions which
are compiled into very efficient machine code. This would be very
difficult for the compiler if the code used explicit repeats and
reshapes instead of broadcasting.
In NumPy and Matlab, this aspect is not equally important. But
broadcasting makes array expressions more terse, and is save some
redundant array copies. NumPy code tends to be memory bound;
broadcasting can therefore improve performance, particularly when arrays
are large. But the effect is not as dramatic as it is in Fortran.
Readability is also important. Vectorized Matlab code quickly
degenerates into a mess of reshapes and repmats, and can be very hard to
read. NumPy and Fortran 90 codes do not loose the readability like this
even in the most complicated array expressions.
Sturla
More information about the NumPy-Discussion
mailing list