c/c++/Fortran efficient indexing
Hey,
If I want to access an integer array several times in a loop is is more efficient to use a variable to hold the integer value, e.g.: int array[]; int k; for(i) { array[i]=k; (k+5)/k+k^4; } rather than for(i) { (array[i]+5)/array[i] + array[i]^4; } ? Thank you! |
Quote:
Modern high optimizing compilers easier to vectorize the code that does not use intermediate variables to store values, in addition with the best opportunity to use prefetch But in any case for specific code need tests and profiling |
Quote:
Regards |
I would say that the 2nd coding style will ultimately generate more efficient code (in my experience)
The rate of a particular code will depend on many factors, such as for small data sets are significant caсhe-effects In any case, I would have tested the performance of several versions of the code PS: It should be noted that the first coding style allows you to write more understandable and readable code that can ultimately will save considerably more time than the modification will be lost at a slower from its execution. The truth is usually somewhere in the middle |
Ok!
I have written lots of code over the years, but I have never had any reason/time to make it more efficient, nor have I had any formal training in writing optimized code. However it just seemed inefficient to repeat several operations during each cycle. A common way to write indexing in CFD is using one array, i.e. u[i+j*iMax], u[(i+1)+j*iMax], u[i+(j+1)*iMax]... This index repeats several times in the difference equations to be solved and writing k=i+j*iMax; u[k], u[k+1], u[k+iMax],... seemed more simple. Anyways, if I really need to optimize the code in the future I will definitely hire some with more knowledge than me in this field ;) |
The truth is that modern optimizing compilers have become so "clever" that "helping" them to optimize their code manually, you just disturb them:)
|
In general, as was mentioned above, I would let the optimizer handle things. Also, in general for a CFD code, you'll see the biggest bang for the buck by minimizing memory bandwidth.
That being said, the code that you've given can maybe made more efficient by writing a2 = array[i]*array[i]; (array[i]+5)/array[i] + a2*a2; instead of (array[i]+5)/array[i] + array[i]^4; |
Quote:
It all depends on the algorithm. Compiler can not optimize all the algorithms. It can however , many times , optimize loops much better than user might do. Few months ago I wrote polygonal polygonal interaction algorithm for GGI. after rewriting the code for performance, the final code could do the same thing in 40% of time of what compiler optimized could manage. My general advise is first go for accuracy and when it is accurate slowly change the code if performance is very important. Faster codes are often difficult to read and understand, so it can make life difficult in future. For this reason it is always useful to keep old code commented out so that it could be helpful in understanding what is going on. |
Quote:
both code have same memory bandwith (will be used 3 memory cells) but first code will be on one multiply instruction shorter Just in this case, pice of code are too short to put in a difficult situation compiler Let's tests ? :) |
Fully agree, we must focus on algorithm optimization and leave code optimizations to compiler
|
Anyway if you are interested in software optimization I would you recommend you this well known series of PDFs http://www.agner.org/optimize/#manuals.
|
All times are GMT -4. The time now is 01:13. |