Strange performance behavior

Marius Muja mariusm at cs.ubc.ca
Wed Sep 19 16:47:44 PDT 2007


I have noticed the following strange (at least for me) performance 
behavior with one of my programs. It is a program that does some 
scientific computations and while trying to optimize it I noticed that 
the code from case_B below executes faster (almost twice as fast) as the 
code in case_A. This is a bit counterintuitive for me, since in case _B 
there is also the cost of the function call (or should be the same if 
the function is inlined).
Can anybody shed some light on why it's behaving this way?

case_A:
-------------------------------
foreach (i,index; indices) {
    foreach (k, inout value; centers[belongs_to[i]])
	value += vecs[index][k];
}
----------------------------------

case_B:
-------------------------------
void addTo(T,U)(T[] a, U[] b) {
    foreach(index, inout value; a) {
       value += b[index];
    }
}
....
foreach (i,index; indices) {
    addTo(centers[belongs_to[i]],vecs[index]);
}
_______________________________


Marius



More information about the Digitalmars-d mailing list