Thread: code optimization

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    10

    code optimization

    How do i write a code to optimize a given code??

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    How long is a piece of string?
    How long does infinity go on for?
    How many stars are there?
    How does Billy Chrystal still get work?

  3. #3
    Registered User -leech-'s Avatar
    Join Date
    Nov 2001
    Posts
    54
    You could do a few things:

    Use define macros instead of functions IF POSSIBLE. For example:
    Code:
    int cube(int x)
    {
    return x * x * x;
    }
    ... could be replaced by:

    Code:
    #define CUBE(x) x * x * x
    You should also look into bit shifting, and there's things you can do with loops.
    Not yet, have to think of one...

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Minimizing the number of instructions in the center of a nested loop always helps, but the best way to optimize code is to have an efficient algorithm in the first place, then you have no need to do piddling code tweaks.

    >#define CUBE(x) x * x * x
    should be
    #define CUBE(x) (x) * (x) * (x)

    -Prelude
    My best code is written with the delete key.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your compiler will likely be able to optimize far better than you can.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    As Prelude states, the most important thing for you as a programmer is to have a very effective algorithm. Other things you could do to increase performance:

    1. Use as much constants as possible so you don't need to recalculate things. Especially in loops performing the same calculations use a lot of time.

    2. Avoid very small functions, such as leech gave for example, replace them by macros. Function calls take a lot of time.

    3. Keep your code as simple as possible.

    In some cases you could use math tricks to improve calculations. For example, when needing to add a lot of numbers:

    1 + 2 + 3 + .. + N = S
    N + .. + 3 + 2 + 1 = S
    (N + 1) + .. + (N + 1) = 2 S

    -> S = N (N + 1) / 2


    Another thing you could do is take a look at the assembly code and do some optimizing, if you have configured your compiler as optimizing, then it will be quite hard. Especially when you have less or no experience in writing/optimizing assembly code.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > How do i write a code to optimize a given code
    1. start with a design
    2. choose the best algorithms to implement that design
    3. write clear code - if it's obvious to use, then it will be to the compiler.
    4. Profile it to make sure you know where the hotspots are
    5. Make damn sure it works before you try 6.

    6. Fiddle around with all the silly tricks, often repeated without foundation, just because someone found a special case with some compiler 10 years ago, and this technique has become an urban legend in how to optimise code.

    Most decent compilers know about all the good optimisations which work (like *2 can be done as <<1, BUT only if its an advantage for that particular machine).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM