|
[Sponsors] |
November 25, 2011, 03:54 |
c/c++/Fortran efficient indexing
|
#1 |
Senior Member
Join Date: Mar 2009
Posts: 151
Rep Power: 17 |
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!
__________________
"Trying is the first step to failure." - Homer Simpson |
|
November 25, 2011, 04:49 |
|
#2 | |
Member
Serge A. Suchkov
Join Date: Oct 2011
Location: Moscow, Russia
Posts: 74
Blog Entries: 5
Rep Power: 14 |
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 |
||
November 25, 2011, 05:10 |
|
#3 | |
Senior Member
Join Date: Mar 2009
Posts: 151
Rep Power: 17 |
Quote:
Regards
__________________
"Trying is the first step to failure." - Homer Simpson |
||
November 25, 2011, 05:23 |
|
#4 |
Member
Serge A. Suchkov
Join Date: Oct 2011
Location: Moscow, Russia
Posts: 74
Blog Entries: 5
Rep Power: 14 |
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 |
|
November 25, 2011, 05:40 |
|
#5 |
Senior Member
Join Date: Mar 2009
Posts: 151
Rep Power: 17 |
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
__________________
"Trying is the first step to failure." - Homer Simpson |
|
November 27, 2011, 19:36 |
|
#7 |
Senior Member
Martin Hegedus
Join Date: Feb 2011
Posts: 500
Rep Power: 19 |
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; |
|
November 27, 2011, 21:10 |
|
#8 | |
Senior Member
Arjun
Join Date: Mar 2009
Location: Nurenberg, Germany
Posts: 1,273
Rep Power: 34 |
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. |
||
November 28, 2011, 00:57 |
|
#9 | |
Member
Serge A. Suchkov
Join Date: Oct 2011
Location: Moscow, Russia
Posts: 74
Blog Entries: 5
Rep Power: 14 |
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 ? |
||
November 29, 2011, 04:39 |
|
#11 |
Member
Join Date: Nov 2011
Location: Czech Republic
Posts: 97
Rep Power: 14 |
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.
|
|
|
|