By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,526 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

Variable Optimization Question

P: n/a
.....I try to reduce un-necessary temporal variables so it can be
optimized for best performance. I try to assign only one register
storage so two variables can access to only one register storage
before they are stored into memory location (variable storage).
.....Please take a look at two functions below. Byte variable and
Carry variable are global variables outside of functions.
.....Do you notice that there are two duplicated lines in Test()
function. First line is the exact same as second line, but it adds
right shift. Before and after optimization, there are no temporal
variable.
.....Test2() function has three lines. First line has one temporal
variable that it is always set to 16 Bits or Word. Second line and
third line are only 8 Bits or Byte. It copies Word (16 Bits) into
register storage before it is right shifted. Carry variable copies
the data (8 Bits) from register storage. It looks like register
storage has 16 Bits, but only low byte will be copied into Byte
variable while high byte will be ignored. It does not require to use
"var & 0xFF", but it only uses "(unsigned char)". After optimization,
temporal variable will be removed, but temporal variable will be used
during the debugging before optimization. Please note that (unsigned
char) might add "var & 0xFF" by adding ADD instruction from C/C++
compiler on other CPU machines except x86 machine.
.....My source code only uses level 4 warning instead of level 3
warning because I want to control (unsigned char) and (unsigned word)
so it can be always bug free.
.....Please suggest which Test() function or Test2() function is best
for readable and stable. I would think to choose Test(), but Test2()
would save my time by reducing minor bug or free bug.
.....What do you think?

Bryan Parkoff

unsigned char Byte = 0xFE;
unsigned char Carry = 0;

void Test(void)
{
Carry = (unsigned char)((Byte + 0x03) >> 8);
Byte = (unsigned char)(Byte + 0x03);
}

void Test2(void)
{
unsigned short Word = Byte + 0x03;
Carry = (unsigned char)(Word >> 8);
Byte = (unsigned char)Word;
}
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I will use Test2() function becuase it is more readable. But using this
optimisations are not good practice. Most of todays compilers will do this
automaticly (or not). If you realy need size optimisation use some asm in
code :) But, all of this is my opinion.

Jul 22 '05 #2

P: n/a
Bryan Parkoff wrote:
....I try to reduce un-necessary temporal variables so it can be
optimized for best performance. I try to assign only one register
storage so two variables can access to only one register storage
before they are stored into memory location (variable storage).
....Please take a look at two functions below. Byte variable and
Carry variable are global variables outside of functions.

<snip>

On -O2 optimisation, gcc produces identical code for both these
functions on my machine. Don't try this kind of optimisation, as
compilers are better at it than you.

Chris
Jul 22 '05 #3

P: n/a


Bryan Parkoff wrote:
....I try to reduce un-necessary temporal variables so it can be
optimized for best performance. I try to assign only one register
storage so two variables can access to only one register storage
before they are stored into memory location (variable storage).
....Please take a look at two functions below. Byte variable and
Carry variable are global variables outside of functions.
....Do you notice that there are two duplicated lines in Test()
function. First line is the exact same as second line, but it adds
right shift. Before and after optimization, there are no temporal
variable.
....Test2() function has three lines. First line has one temporal
variable that it is always set to 16 Bits or Word. Second line and
third line are only 8 Bits or Byte. It copies Word (16 Bits) into
register storage before it is right shifted. Carry variable copies
the data (8 Bits) from register storage. It looks like register
storage has 16 Bits, but only low byte will be copied into Byte
variable while high byte will be ignored. It does not require to use
"var & 0xFF", but it only uses "(unsigned char)". After optimization,
temporal variable will be removed, but temporal variable will be used
during the debugging before optimization. Please note that (unsigned
char) might add "var & 0xFF" by adding ADD instruction from C/C++
compiler on other CPU machines except x86 machine.
....My source code only uses level 4 warning instead of level 3
warning because I want to control (unsigned char) and (unsigned word)
so it can be always bug free.
....Please suggest which Test() function or Test2() function is best
for readable and stable. I would think to choose Test(), but Test2()
would save my time by reducing minor bug or free bug.
....What do you think?

Bryan Parkoff

unsigned char Byte = 0xFE;
unsigned char Carry = 0;

void Test(void)
{
Carry = (unsigned char)((Byte + 0x03) >> 8);
Byte = (unsigned char)(Byte + 0x03);
}

void Test2(void)
{
unsigned short Word = Byte + 0x03;
Carry = (unsigned char)(Word >> 8);
Byte = (unsigned char)Word;
}


good compilers aren't going to generate vastly different code for these.
I would choose the second because it is easier to maintain. The first is
easier to create since you can cut and paste.

David
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.