Re: elemental function versus loop over function



Mark Morss <mfmorss@xxxxxxx> wrote:

I have to perform a scalar function on each of the elements of a large
array. In general, for speed, does it matter much whether I loop over
the array or define an elemental function?...

Given the usual caveat (test if you want to be sure), I'd expect the two
methods to be darned close to the same. To my knowledge, most
implementations pretty much implement elemental by doing the loops
behond your back.

Of course, this presumes a serial machine. In a parallel environment,
things would be different, though it still wouldn't surprise me if the
two methods of writing it ended up largely equivalent. I've little
personal experience in that area.

In my view, the main reason for writing elemental procedures is for
simplicity of code rather than for performance. That applies
particularly when you have a procedure that is applied to scalars and
arrays of multiple ranks. It can be a mess to write what amount to
multiple copies of the same code to handle the multiple ranks.

A disadvantage of elementals is that they have stringent restrictions,
which occasionally get in the way. For example, they must be pure, which
disallows all kinds of things. There is a proposal to allow what might
be called (but I don't think it is) impure elementals in the future.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
.