.....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;
} 3 1693
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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
print "x:", x
f()
|
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 second name for better
readable. After optimization, global variables might be misaligned
because each global variables must be converted to 32 bits, but I do
see that C/C++ Compiler do padding between variables. Struct does the
same to do padding....
|
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 with alignment by optimization, it would be
great when I write global variable inside namespace group. It has no impact
with the performance between global variable and struct, but they are always
the same.
Bryan Parkoff
|
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 compiler for the
embedded target.
My question is about how compilers optimize certain code sequences.
As an example, take the code below. Will the compiler eliminate the
actual function call to foo() in the object code generated and just
store...
|
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
{
| |
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 = 3;
I know its setting aside storage for the variable itself; does it also use
up more storage if the variable name is longer? I realize it would probably
take quite a lot of long variable names to make any impact if so, but I was
just curious...
|
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.
Consider the following test program
/* begin foo.c */
#include <stdio.h>
#include <string.h>
|
by: Christian Meier |
last post by:
Hello NG
I have the following code:
file1.h:
static const int iValue = 5;
<EOF>
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |