I need to perform lots of Vector computations efficiently.
Suppose we need to compute
Vres=a*V1+b*V2+c*V3
where
Vres, V1, V2 and V3 : Vectors,
a,b,c: constants.
Which is a better code in terms of the cpu time
1) Vres=V1*a+V2*b+V3*c
or
2) for(i=0;i<Nsize;i++) Vres(i)=V1(i)*a+V1(i)*b+V1(i)*c ?
The first one includes 3*Nsize loops for multiplication
and others for addition,
while the latter one includes Nsize loop but with
the access to the vector component.
Regrads,
Kazuya
Question on Vector computation
Moderators: silvia, selimgunay, Moderators
the first involves 5 calls to the matrix constructor & 5 subsequent calls to the destructor .. lots of memory calls to the os typically kill object-oriented programs (it's why we use references wherever we can).
the second involves all those calls to the inline functions .. while not great .. probably better .. of course how good depends on how good the compiler is at inlining the function.
a third option and the best would be to use one of the vector methods.
Vres.addVector(0.0, V1, a);
Vres.addVector(1.0, V2, b);
Vres.addVector(1.0, V3, c);
the second involves all those calls to the inline functions .. while not great .. probably better .. of course how good depends on how good the compiler is at inlining the function.
a third option and the best would be to use one of the vector methods.
Vres.addVector(0.0, V1, a);
Vres.addVector(1.0, V2, b);
Vres.addVector(1.0, V3, c);
Vector computation
I tried third option.
Although I did not measure the cpu time,
it seemed that this option was the fastest one.
Thank you
Although I did not measure the cpu time,
it seemed that this option was the fastest one.
Thank you
Vector computation
I tried third option.
Although I did not measure the cpu time,
it seemed that this option was the fastest one.
Thank you
Although I did not measure the cpu time,
it seemed that this option was the fastest one.
Thank you
Re: Vector computation
Frank, can you explain more why the third is the fastest? I am interested
to learn.
to learn.