1. 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. 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;`
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.

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.

3. 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

4. You may be counting toothpicks, while 4 X 10 lumber is going right by you.

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

6. Wouldn't it be a good idea to use a smaller type than int? Since you're flags can be 1 or 0?

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

8. Ahh... I need to read original posts more clearly. I thought he was wanting to make his code use less memory (RAM).

9. 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.

10. 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.

11. How many string constants do you have in the code?

Any other initialised arrays?