3 ms·
C++'s standard library has valarray, which is supposed to avoid aliasing effects, and is allowed to use expression templates and other tricks. Something like:
by pbsd 13y ago
C++'s standard library has valarray, which is supposed to avoid aliasing effects, and is allowed to use expression templates and other tricks. Something like:
float dot(std::valarray<float> const& x)
{
return (x*x).sum();
// Alternative: inner_product(begin(x), end(x), begin(x), 0.0f);
}
should, in theory, achieve the same kind of performance as the C version (but none of this is guaranteed, sadly). A smart implementation would actually end up calling BLAS routines, which is what libraries like Eigen do.
Which reminds me: does Rust have infrastructure that would allow something like expression templates to exist?
- pcwalton 13y agoI believe you can use Rust traits to the same effect, although I haven't tried so you might hit stuff.