Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: A guide to optimizing C++

  1. #1
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default A guide to optimizing C++

    I cannot provide all the methods of optimizing C++ code, so I ask all the experienced coders here on TJ to give there tips. Binary searches, look-up tables, pointers, registers, assembly code, you name it; if it does optimizations then it is welcome. Post it here.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  2. #2
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Ok I will start of by given the source code of a function that converts the format of a random input stream and the timing of the conversion. (When I get back.) This can be optimised with regards to speed by anyone. Then they must give the reason why there code is improved.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  3. #3
    Join Date
    Jul 2005
    Posts
    75
    Rep Power
    0

    Default

    Well i have a very important tip for "optimizing C++".

    Make sure you properly define the problem and make your solution as generic as possible.

    Also always document your code properly.

    Well if i think of any other things i will post them..............
    "A computer once beat me at chess, but it was no match for me at kick boxing." - Emo Philips -

  4. #4
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Ok................................................ ....................
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  5. #5
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Quote Originally Posted by Mr. Unstoppable
    Well i have a very important tip for "optimizing C++".

    Make sure you properly define the problem and make your solution as generic as possible.

    Also always document your code properly.

    Well if i think of any other things i will post them..............
    I agree with all of this perfectly. I will expand on "defining the problem", "generic solutions" but not on "documentation", though I do agree that documentation is very useful.

    I am preparing a list of optimizing tip and I will post examples on each topic over time.

    I am posting because, sometimes I have to learn what I do not know by first learning how little I know. I would be glad for your input.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  6. #6
    Join Date
    Jul 2005
    Posts
    75
    Rep Power
    0

    Default

    Well Crosswire i do think it is important that you expand cause alot of people might now understand that there are different types of optimization of programming. Not just LOC, but effieciency, readability, industry defined good programming practices etc.

    I could also get into a few of those but i think you would do a superb job so i leave the floor open to you.
    "A computer once beat me at chess, but it was no match for me at kick boxing." - Emo Philips -

  7. #7
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Other Links
    http://www.codeproject.com/cpp/C___C...timization.asp
    There is a good link.

    About Optimization
    The optimization, I am referring to is the runtime speed of the program. Think FPS. I am not speaking of memory optimization, bandwidth optimization, development time optimization or any other kind of optimization unless it increases the raw speed of an application. It is like tuning up a car engine to make it run as fast as possible.

    Why optimization is important
    I particularly don’t use optimization much because the process is time consuming. However, there are times when the need for optimization is high. Take for example, developing a game to increase fps, or developing a research program that takes many hours before returning the result. Due to laziness and lack of development time I can find ways to bypass these problems, such as running a research program overnight while I sleep, or develop a game using lesser graphics and physics data. Eventually I will have to put more data in a game or run a longer research program. These stress the need for optimization.

    How important is optimization
    Even when optimization is needed it is good to have an idea of the amount of optimization that you will achieve from a particular code change e.g. 1%, 5% or 10% increase in speed. This can give you an idea of the worth of the optimization, and if that is worth your efforts.

    When optimization is needed but not used
    For testing purposes, when you are testing the speed of a particular module, you can call that function a lot of times in an unoptimised way.

    Factors Affecting Optimization
    * The type of processor architecture
    * The complier (different compliers build code with very small variances)
    * The build settings (a compiler can build different code from the same source depending on the build settings)
    Run time is affected by:
    * The OS
    * Background applications and free memory

    Automatic Optimizations
    Compilers can optimize code, e.g. remove unused logic. Sometimes it sets up assembly instructions so that the processor has a better means of executing parallel instructions and pre-fetch other instruction and avoid lags. Sometimes it takes stretched out code and reduce it to compact assembly instructions by organizing them and making multiple operations on a single data before it is done with that data. The source code may need to be written more compact or organized. Not all compilers do automatic optimization the same way. Personally, I do not mess with this level of optimization; if I am that pressed for speed then I would spend the extra time and effort in assembly language; I will try to do as much low level optimization in future posts.
    Last edited by crosswire; Aug 17, 2005 at 06:15 AM.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  8. #8
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Tricks to Optimization

    I will add more to the list, just need to do the reading.
    Please, anyone, help me expand my list
    Last edited by crosswire; Aug 16, 2005 at 03:39 PM.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  9. #9
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    can you explain these or point us to an article ...

    Quote Originally Posted by crosswire
    • Using the ‘this’ pointer - What is the effect of this?
    • Lite code – I wonder how much the size of an executable affects it performance and the processor cache
    • Patching the executable that is loaded memory at run time – This is an idea I think is possible with a software cache, not with a hardware cache, but I need to research on it to see how it is done. It is not much different than calling a more optimized block of code depending on the state of the program.
    • Threading – If you are running multiple threads, use a method to organize their efficiency like a thread pool.
    and some i think are targeted at my code...( what exactly do you term overuse?)
    Quote Originally Posted by crosswire
    • Using multiple indexes – What is the effect of this?
    • Over-using multiple pointers in one line of code – What is the effect of this?
    • Over-using multiple indexes – What is the effect of this?
    • Using multiple pointers in one line of code – What is the effect of this?
    i think the c/c++ language is supposed to short circuit by default in cases like the one below...
    Quote Originally Posted by crosswire
    • Break when a condition will not be met – if(a)if(b)if(c) instead of if(a && b && c) is optimize. People who know this may forget about it.
    Most of them are good points i already knew. i saw them on an article on optimisation, I tried to find it back but i cant. i'm still trying though. i didnt want to tel you guys al that stuff and make it sound like mine, so as soon as i find it you'll see why i mostly start my loops at the top and decrement... for(int ix = MAX; ix-- ; ) code = something();
    until then i hope everyone understands (and applies ) what you said.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  10. #10
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Quote Originally Posted by icymint3
    i think the c/c++ language is supposed to short circuit by default in cases like the one below
    you think.. I would hope so, can't imagine it wouldn't on such a statement if(a && b && c) wouldn't need to completely evaluate if a was false. Anyways good points crosswire too bad I don't got any use for C/C++ anymore.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •