On 6 Aug, 14:54, Giovanni Gherdovich

<(E-Mail Removed)> wrote:

> Hello,

>

> my question is pretty much related to this topic so I continue

> the thread instead of opening another one.

> I'm a former Matlab coder and a wannabe C++ coder.

>

> I've read about std::valarray<> in the Stroustrup's book

> "The C++ Programming Language", discovering that some operators

> (like the multiplication *), and some basic math functions

> like sin and cos, are overloaded in order to behave very similar

> to Matlab ones when dealing with valarrays (mainly, they act

> component-wise).

>

> I'm aware of Matlab "vectorization" techniques; I use them to

> avoid for-loops.
That's a *matlab* problem. 'Vectorization' is a concept

exclusive to matlab, which historically was caused by

what I consider to be bugs in the matlab interpreter.

> But when I do that I _don't_ do linear algebra: I just do

> component-wise operations between matrices, in order to

> save Matlab from doing serial calls to the same routine (via for-

> loops).

> I dont't think such code gains anything from using BLAS or

> similarities.

> I mean: taking the inverse of a matrix is linear algebra,

> but multiplying two vectors component-wise is just... multiplying.

>

> rocksportrocker

>

> > If it is for performance: Writing native loops in C/C++ with some

> > optimization flags will give you best performance in most cases.

>

> Best performance than Matlab,
Depending on exactly what you do, matlab *can* get very

close to best-possible performance since it uses highly

tuned low-level libraries. If your operation is covered by

such a function, you might find it difficult to beat matlab.

If not, don't be surprised if C++ code beats matlab by

a factor 5-10 or more.

> or best performance than

> "vectorized" C++?
There is no such thing as 'vectorized C++.'

> Rune Allnor:

>

> > The problem is that users who only know matlab and no other

> > programming languages are conditioned to believe that the

> > problem lies with for-loops as such, and not with matlab.

>

> My question:

> When writing C++ code, do you thing I can have faster code

> if I use std::valarray<> in "the Matlab way", instead

> of using, say, std::vector<> and for-loops?
I don't know. I haven't used std::valarray<>. I know I have

seen some comment somewhere that std::valarray<> was an early

attempt at a standardized way to handle numbercrunching in C++,

which was, well, not quite as successful as one might have

whished for.

> I must admit that this curiosity comes from my previous Matlab

> experiences, when I used to think to for-loops as being the devil,

> but from a naive point of view I can image that "vectorized"

> operations on std::valarray<> are optimized by smart compilation

> techniques...
You would be surprised: There are for-loops at the core of all

those libraries, even the BLAS libraries matlab is based on.

There are smart compilation techniques involved, but to *optimize*

the for-loops, not to *eliminate* them.

> after all the programmer doesn't specify the

> order with which the operation as to be done, like in a foor-loop...
I wrote a sketch to illustrate how this is done in a previous

post in this thread, which was posted only to comp.soft-sys.matlab:

http://groups.google.no/group/comp.s...d663e1fa?hl=no
As you can see, the 'vector' version myfunction(std::vector<double>)

calls the scalar version myfunction(double) in a for-loop. This is

essentially what is done in all the libraries you use, including

matlab.

The for-loops are at the core, and the smart compiler techniques

optimize the executable code to avoid any unnecessary run-time

overhead.

> Is this just fantasy?
You might want to have a look at the basic texts on modern C++.

Try "Accelerated C++" by Koenig & Moo, or "You can do it!" by

Glassborow. Or both.

Rune