nw said:
What is it about valarray that allows it to be aggressively optimised?
Is it because it has a fixed size? So that a compiler could choose to
store it in a small but fast region of memory and be sure that it
would be necessary to grow the array?
I'm trying to understand why I need this data structure, as others
have noted it's usually not well optimised and doesn't seem to have
been developed very well in the standard.
Have a look on the entire chapter 22 of TC++PL3, it provides some info
on this.
For example on page 663 it is mentioned:
"The valarray and its auxiliary facilities were designed for high-speed
computing. This is reflected in a few constraints on users and by a few
liberties granted to implementers. Basically, an implementer of valarray
is allowed to use just about every optimization technique you can think
of. For example, operations may be inlined and the valarray operations
are assumed to be free of side effects (except on their explicit
arguments of course). Also, valarrays are assumed to be alias free, and
the introduction of auxiliary types and *the elimination of temporaries*
is allowed as long as the basic semantics are maintained. Thus, the
declarations in <valarray> may look somewhat different from what you
find here (and in the standard), but they should provide the same
operations with the same meaning for code that doesn't go out of the way
to break rules. In particular, the elements of a valarray should have
the usual copy semantics (17.1.4)".
I do not know if you need valarray though. This is up to you to decide.
vector is efficient for most applications.
Have a look on 17.1.2 of TC++PL3 where the costs of the various
containers are mentioned.