Code Efficiency

This is a discussion on Code Efficiency within the C Programming forums, part of the General Programming Boards category; I am working on a program where the code exceeds the ROM space by ~1K. I'm trying to reduce the ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    35

    Code Efficiency

    I am working on a program where the code exceeds the ROM space by ~1K. I'm trying to reduce the size of my program.

    There is a lot of code that checks the state of a binary Flag.

    Code:
    int flag = 1;
    
    do something; //This may change the state of flag.
    
    if (flag == 0) do something;
    else
    do something else;
    Since the state of the flag can be 1, or 0, I can write the decision block as:

    Code:
    if (flag == 0) do something;
    
    or
    
    if (flag != 1) do something;
    
    or
    
    if (flag < 1) do something;
    Also which is better.

    Code:
    int zero = 0;
    int flag = 1;
    
    do something; //This may change the state of flag.
    
    if (flag == zero) do something;
    else
    do something else;
    or

    Code:
    int flag = 1;
    
    do something; //This may change the state of flag.
    
    if (flag == 0) do something;
    else
    do something else;
    This code is repeated many times using different binary variables for the flag function, So a few bytes savings is multiplied.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by danlee58
    Since the state of the flag can be 1, or 0, I can write the decision block as:
    This seems best to me:
    Code:
    if (!flag) do something;
    Quote Originally Posted by danlee58
    Also which is better.
    I cannot see how defining an additional variable is better than using a constant, but really, since the output depends on the compiler, the only way (other than figuring out the nitty gritty of the compiler) to know for sure is to compile and check.

    Quote Originally Posted by danlee58
    This code is repeated many times using different binary variables for the flag function, So a few bytes savings is multiplied.
    It seems to me that you can get more savings by figuring out how to "roll" the repeats into a loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The only way to know for sure which is most efficient is to compile the different alternatives and see what difference it makes.

    It is very likely that there will be little or no difference at all. Generally speaking, processors compare with 0 "easier" than with other numbers, and with small numbers easier than large numbers (simply because small numbers can be stored in a compact form, whilst large numbers obviously take up more space). How large a "small" number is depends on the processor architecture - often a byte is "small" and "anything else" is large, but there is no firm rule here.

    Note that if (flag) and if (!flag) are likely be the most efficient options if the compiler is a little bit stupid (if it's clever, it won't make it worse, nor better, compared to if (flag == 1) or some such).

    You may also want to look at compiler options such as "optimize for size", and try the different optimization levels to see which is most space efficient - some higher optimization levels sometimes produce MORE code than lower levels.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You may be counting toothpicks, while 4 X 10 lumber is going right by you.

    Post your code.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I agree with Adak.

    Also, how large is the total source? 1KB removal out of 2KB is probably very difficult to achieve, whilst 1KB out of 64KB of code is likely to be "easy" relatively speaking.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Wouldn't it be a good idea to use a smaller type than int? Since you're flags can be 1 or 0?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bladactania View Post
    Wouldn't it be a good idea to use a smaller type than int? Since you're flags can be 1 or 0?
    That MAY give smaller code, but far from always. For example, x86 in 32-bit mode has smaller code for 32-bit int than it does for short int, as a general rule, because the "short int" code needs a 0x66 prefix to tell the processor that "this is a 16-bit operation".

    And before anyone points it out, yes, also 8-bit (char) types are smaller than 16-bit.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Ahh... I need to read original posts more clearly. I thought he was wanting to make his code use less memory (RAM).

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    I'd like to think I'm pretty good at reducing code size. I'm always shortening existing code, or pointing out ways of shortening code to others, especially at work. However, one cannot possibly do that from such tiny snippets of code. If you really want someone else to make a difference you must post decent chunks of the real code.
    If you cannot or will not do that, then that is most unfortunate and you'll have to see what you can do yourself, by trial and error.

    So far this is like asking for different ways to fold up one handkerchief when the goal is to get everything to fit in your suitcase.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    35
    The ROM size is 32K. I am whittling away a what was > 1K more than the 32K. I asked the question to get some insight into the best way to code these decision blocks. They may not seem like much, but they occur many times in my code. Any small savings in one snippet of code will be multiplied many times.

    In the meantime I am tackling the larger questions, and have reduced the problem to < 500 bytes.

    Now I will try the !flag.

    Thanks for the help.

    BTW, Setting an integer 'one = 1', and using 'flag == one' seems to produce less code than repeating 'flag ==1'. Possibly because it's a memory operation.
    Last edited by danlee58; 02-27-2009 at 07:12 PM.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    How many string constants do you have in the code?

    Any other initialised arrays?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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