How do I write more efficient code?

This is a discussion on How do I write more efficient code? within the C++ Programming forums, part of the General Programming Boards category; Obviously it is important for any programmer to make his/her code as efficient as possible. But are there any guidelines ...

  1. #1
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801

    How do I write more efficient code?

    Obviously it is important for any programmer to make his/her code as efficient as possible. But are there any guidelines that anyone can give?

    So for instance, will my code be more efficient if I do the following?

    Use fewer variables
    Use fewer lines

    For Instance, is:
    Code:
    for (int i=0;x;i++)
    {
    //do stuff to x
    }
    better than:

    Code:
    for (int i=0;;i++)
    {
    //do stuff to x
    if(x==0)
    break;
    }
    Make many functions dedicated to simple tasks to avoid code repetition

    So if I have:

    Code:
    if (x>y)
    {
    z=1;
    }
    else
    {
    z=0;
    }
    more than once is it always better to do:

    Code:
    z=compare(x,y);
    
    //where compare() is:
    
    int compare(int x,int y)
    {
    if (x>y)
    {
    return 1;
    }
    return 0;
    }

    Is it dependent upon the intelligence of your compiler (I use MSVC++)?

    So for instance will:

    Code:
    s1 = s1 + s2 + s3 + s4;
    and:

    Code:
    s1 += s2;
    s1 += s3;
    s1 += s4;
    be recognised by the compiler as the same and therefore converted to the same machine code?

    Can anyone gives any tips? Or point me to some info? I have tried googling but didn't get very much.

  2. #2
    moonwalker_
    Guest

    here

    instead of...
    Code:
    for (int i=0;;i++)
    {
        //do stuff to x
       if(x==0)
       break;
    }
    Why cant you use a while loop instead of this one..
    [code]
    while(1)
    {
    //do stuff to x
    if (x==0)
    {
    break;
    }
    }
    [code]

    or simply,

    Code:
    while(x != 0)
    {
    	//do stuff to x
    }
    There are many other things that you can do..

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Try reading the book: Efficient C++

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Another good book is:
    Programming Pearls Second Edition by Jon Bentley

    It discusses algorithms, data structures, and code optimization. It's been around for a while, but it is a classic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  3. Replies: 6
    Last Post: 04-04-2003, 09:09 PM
  4. How to write code to change the printer resolution
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 07-26-2002, 06:04 PM
  5. more efficient code
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-11-2001, 01:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21