1. optimization

Dear C++ programmers,
Few weeks ago I started a thread about optimizaion of programms. Unfortunately this thread cannot be retireved any more so I would like to ask you again for few useful keywords related to the subject. Also someone gave me link to the page that explains some mathematical theory (there were some graphs with curves in it) that quantifies optimization, so I would kindly ask to post that link again because I did not bookmarked it.
Thank you 2. You can optimize three different ways. One way is code optimization, another is logic optimization, and the last is data optimization. Code optimization is stuff like using pointers instead of indexes:
Code:
int array;

// slower
for ( int i = 0; i < size; i++ ) {
sum += array[i];
}

// faster
int *p = array;
int *end = array + size;

while ( p != end ) {
sum += *p++;
}
, reversing and unrolling loops:
Code:
int array;

// slower
for ( int i = 0; i < size; i++ ) {
sum += array[i];
}

// faster
for ( int i = 0; i < size / 2; i += 2 ) {
sum += array[i] + array[i + 1];
}
, and breaking early out of loops when the work is done or skipping over iterations when you don't need to do anything that time around. Logic optimization is picking better algorithms for what you're doing. Like using quicksort if you have a lot of things to sort, but bubblesort when you only have a hundred or so. Data optimization is picking the right data format to be fast, like when you can design your own import file. It's also data optimization when you pick the source of the data to be fast, like using a file instead of interactive input, or piping from another program instead of a file. 3. Code optimization is stuff like using pointers instead of indexes:
Last discussion proved that approach to be irrelevant when using modern compilers with full optimization...

Manual loop unrolling can have a negative effect due to preventing the automatic optimization algorithms from working properly...

In most cases the code optimization should be applied only after the careful profiling and proofing the compilers optimization fails to provide the optimal code for some hot-spot...

Careful choosing of datastructures suitable to the task together with algorithms used can provide a lot more boost in the performance when local code optimization espesially if it is done based on the antient strategies not suitable for modern compilers and processors. 4. Here's one link to a page on optimisation.

A few days ago I wrote some code to draw a Mandelbrot fractal, where each pixel's colour gets updated a number of times based on the results of a calculation. My first (naive) approach was to keep and update the RGB values for each pixel individually. This turned out to be painfully and visibly slow.
I then set up a colour table for the whole image and just kept track of an index into this table for each pixel. Much better, the program ran several times faster.

The initial approach was stupid, of course, but this is the kind of a place where I'd optimize, as it makes a feelable difference. And the optimisation that really worked was rewriting the data structures. 5. Thank you for answers. I would just like to ask if "nonlinear optimization" can be helpful in writing better programs? 6. Non-linear optimisation is a mathematical technique. In principle, yes, it could be helpful in writing better programs. Doing that would require you to formulate a mathematical equation that relates all attributes of your program (design, implementation techniques, etc) to some performance matric (eg speed, development time, etc) so you can apply non-linear optimisation techniques to find the "optimal" programming approach. In practice, that would be exceedingly difficult because the development of that equation requires complete knowledge of everything: the requirements, the design techniques, the coding techniques, the productivity of the programmer, etc etc. If anyone had such complete knowledge, we wouldn't need to bother about optimising -- all of the world's problems would be solved as an incidental side effect.

While you won't necessarily find the topic in a literature search, you might want to ponder the notion of "premature optimisation" -- the wasteful act of optimising something before there is a measurable or demonstrable need.   Popular pages Recent additions 