Thread: optimization

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    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. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    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[10];
    
    // 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[10];
    
    // 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. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    50
    Thank you for answers. I would just like to ask if "nonlinear optimization" can be helpful in writing better programs?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    50
    Grumpy, Thanks a lot for your insightful answer.
    I would like to learn few comments abou two more key-words that I recently found:
    1) refactoring
    2) best practices.
    Can they be useful in writing better programms?
    Thank you

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Refactoring is useful: code refactoring is (IIRC) literally "any change to a computer program that improves readability or simplifies structure while still generating the same required behaviour".

    Best practice is a generic term. Using that as a keyword search will turn up some useful information related to software development (eg best practice coding techniques) but it will also turn up some irrelevancies (eg best practice advertising).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Turn Off Optimization?
    By danlee58 in forum C Programming
    Replies: 6
    Last Post: 12-10-2008, 03:52 AM
  2. need reading material for c++ database optimization
    By elninio in forum C++ Programming
    Replies: 0
    Last Post: 07-24-2008, 11:32 PM
  3. optimization flags
    By markucd in forum C++ Programming
    Replies: 4
    Last Post: 06-30-2006, 09:08 AM
  4. Optimization settings
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-23-2005, 02:53 AM
  5. Optimization stuff
    By jverkoey in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2004, 06:02 AM