473,387 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Variable Optimization Question

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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
0
by: Bryan Parkoff | last post by:
I know that after optimization is done under C++ compiler's option, it always align byte and word into dword inside struct. What about global variable in this slope? If global variable has done...
14
by: joshc | last post by:
I'm writing some C to be used in an embedded environment and the code needs to be optimized. I have a question about optimizing compilers in general. I'm using GCC for the workstation and Diab...
5
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
16
by: John | last post by:
Does the length of my C variable names have any affect, performance-wise, on my final executable program? I mean, once compiled, etc., is there any difference between these two: number = 3; n =...
37
by: Erwin Lindemann | last post by:
If a VLA appears within a loop body, it seems the behavior is different with two different compilers I tried. I looked at the standard text, but couldn't find a definite answer there either. ...
21
by: Christian Meier | last post by:
Hello NG I have the following code: file1.h: static const int iValue = 5; <EOF>
20
by: Ravikiran | last post by:
Hi Friends, I wanted know about whatt is ment by zero optimization and sign optimization and its differences.... Thank you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.