[ C code ] - Unexplainable drastic performance improvement

living.thematri

Registered
Hi!

I'm a computational chemist and I did a C-code which spent much of its time inside a triple loop.
These loops compute a variable, "sjk", very fast (less than 2 sec.) if I don't put OUT of the loops any instruction like:

printf("%f",sjk);

But, if I try to visualize this variable with a printf, the code runs very much slower (> 1000 sec.).
I tried to rename it out of the loop etc... I always have the same problem.
I discover that if "sjk" is declared as a global one, the program runs slowly also if I don't print it.
I think the problem could be some optimization option of the compiler (I compile with -fast) which can not switched on when I try to print "sjk" or I use it as a global variable.
Do anyone know the reason (and possibly a solution) of my problem?
Thank you!

living.thematrix@gmail.com

for(i=0;i<natoms;i++){
for(j=rows_xyz[0];j<=rows_xyz[1];j++){
xloc=x[j];
yloc=y[j];
zloc=z[j];
sjk=0.;
for(k=rows_xyzls[0];k<=rows_xyzls[1];k++){
D[0]=xloc-xls[k];
D[1]=yloc-yls[k];
D[2]=zloc-zls[k];
D[0]=D[0]*D[0];
D[1]=D[1]*D[1];
D[2]=D[2]*D[2];
D[2]=D[0]+D[1]+D[2];
D[2]=sqrt(D[2]);
if (D[2]!=0.){
D[2]=1./D[2];
sjk=sjk+D[2]*lagwj[k];
}
}
sjk=sjk*pinv;
}
}
 
Well, as you have found out, global variables cannot be as optimized as local variables. Unless you need a global variable, don't declare one.

Printing to the stdout is always going to be slow. Therefore, you may want to print to a file instead. That's what I normally do when I want to debug my programs and print the value of the variables to a file for inspection later.
 
Any code that uses printf (or the equivalent) to output during huge loops will be slower, because if you're executing it with the Terminal or Xcode, the output window has to play catch up displaying all those debugging messages.
 
Back
Top