micro-optimization question

C

Cross

Hello

I have question about using a signed and unsigned int data types. In a typical
for loop like:

int i;
for(i=0;i<n;i++);

If I used an unsigned int instead of an int, a difference of how many
instructions is achieved? Would it matter if we were considering embedded systems?

Regards,
Amitav
 
B

Ben Pfaff

Cross said:
I have question about using a signed and unsigned int data
types. In a typical for loop like:

int i;
for(i=0;i<n;i++);

If I used an unsigned int instead of an int, a difference of
how many instructions is achieved? Would it matter if we were
considering embedded systems?

My guess is that the difference is zero instructions, in both
cases.

Test it and see.
 
J

Jean-Christophe

I have question about using a signed and unsigned int data types. In a typical
for loop like:
int i;
for(i=0;i<n;i++);
If I used an unsigned int instead of an int, a difference of how many
instructions is achieved? Would it matter if we were considering embedded systems?

If the variable 'n' is 'int' - i.e, 'signed int' - there is no problem
here.

If your system codes an integer value into 16 bits,
an 'unsigned int' will span from 0x0000 (0) to 0xFFFF (65535),
while a 'signed int' will span from 0x0000 (0) to 0x7FFF (32767)
and 0x8000 to 0xFFFF will represent negative numbers.

Therefore, the value 0xA000 will be positive for an
'unsigned int', and negative for a 'signed int' ...

It is to you to know what you are doing.

Now, about the number of instructions involved at runtime,
there is no difference between 'signed' or 'unsigned',
because for a given data type (i.e, 'int') they are of the same size.

Here are some examples of code :



// This will work ---------------

#define MAX 1000

int i;

for( i = -1; ++i < MAX; )
{
// some code
}

// This will *NOT* work ---------------

#define MAX 1000

unsigned int i;

for( i = -1; ++i < MAX; )
{
// some code
}


// This will work ---------------

#define MAX 30000

int i;

for( i=0; i < MAX; i++ )
{
// some code
}

// This will work ---------------

#define MAX 30000

unsigned int i;

for( i=0; i < MAX; i++ )
{
// some code
}

// This will work ---------------

#define MAX 60000

unsigned int i;

for( i=0; i < MAX; i++ )
{
// some code
}

// This will *NOT* work ---------------

#define MAX 60000

int i;

for( i=0; i < MAX; i++ )
{
// some code
}
 
P

Peter Nilsson

Jean-Christophe said:
...
// This will *NOT* work ---------------

#define MAX 1000

unsigned int i;

 for( i = -1; ++i < MAX; )
 {
   // some code
 }

In what sense will it '*NOT* work'?

// This will *NOT* work ---------------

#define MAX 60000

int i;

 for( i=0; i < MAX; i++ )
 {
   // some code
 }

It will work on a great many machines. What you're trying
to say is that it isn't generally portable though since
INT_MAX is not required to be larger than 32767.
 
U

user923005

Hello

I have question about using a signed and unsigned int data types. In a typical
for loop like:

int i;
for(i=0;i<n;i++);

If I used an unsigned int instead of an int, a difference of how many
instructions is achieved?  Would it matter if we were considering embedded systems?

Tragically, the Poleaxe machine has not been perfected.

Be that as it may, the danger lies in going the other direction:

unsigned i;
for (i = n; i >= 0; i--);

If you choose the sign of a loop counter data type because you think
that signed or unsigned will be faster, then you are a silly, silly
man.
Unless you are a woman. In which case you are a silly, silly one of
those.

Surely this is a troll. Yes, it simply has to be.
 
T

Tony

Cross said:
Hello

I have question about using a signed and unsigned int data types. In
a typical
for loop like:

int i;
for(i=0;i<n;i++);

If I used an unsigned int instead of an int, a difference of how many
instructions is achieved? Would it matter if we were considering
embedded systems?

Well you got the subject exactly right in using the words
'micro-optimization'. If you do a search on unsigned vs. signed you'll find
many threads debating which is better to use, but performance was never a
concern.
 
T

Thad Smith

Cross said:
Hello

I have question about using a signed and unsigned int data types. In a typical
for loop like:

int i;
for(i=0;i<n;i++);

If I used an unsigned int instead of an int, a difference of how many
instructions is achieved? Would it matter if we were considering embedded systems?

The difference between generated code for signed and unsigned ints,
especially for small processors, is usually found in multiply, divide,
and shifts, and perhaps array indexing, not addition and subtraction.

The bigger effect, though, for 8-bit processors is the choice of int or
char for variables needing 8 bits or less.
 
C

Cross

Tony said:
Well you got the subject exactly right in using the words
'micro-optimization'.
Well I named the topic such that after reading my question people don't have to
comment that it is a micro-optimization question. I tried to make it clear-cut
so that people not interested don't waste their time.
If you do a search on unsigned vs. signed you'll find
many threads debating which is better to use, but performance was never a
concern.
I shall check them out.
 
C

Cross

Peter Nilsson wrote:
It will work on a great many machines. What you're trying
to say is that it isn't generally portable though since
INT_MAX is not required to be larger than 32767.
Thanks Peter for the clarification; but I already got his point.
 
C

Cross

Jean-Christophe said:
If the variable 'n' is 'int' - i.e, 'signed int' - there is no problem
here.

If your system codes an integer value into 16 bits,
an 'unsigned int' will span from 0x0000 (0) to 0xFFFF (65535),
while a 'signed int' will span from 0x0000 (0) to 0x7FFF (32767)
and 0x8000 to 0xFFFF will represent negative numbers.

Therefore, the value 0xA000 will be positive for an
'unsigned int', and negative for a 'signed int' ...

It is to you to know what you are doing.

Now, about the number of instructions involved at runtime,
there is no difference between 'signed' or 'unsigned',
because for a given data type (i.e, 'int') they are of the same size.
I understand they are of the same size; just wassn't sure about the instructions.
 
N

Nick Keighley

Subject: "micro-optimization question"

Well I named the topic such that after reading my question people don't have to
comment that it is a micro-optimization question. I tried to make it clear-cut
so that people not interested don't waste their time.

yes, but you failed to get the point that micro-optimisation is the
term used for an unnecessary tiny "optimisations". The real answer to
your
question is look at the assmebler or time it. Be aware that any answer
you
get will have to re-validated for each platform you use (and
"platform"
may be a compiler version or even a different set of compiler flags).

You are probably wasting your time. Except you may learn not to do
micro-optimisation.

<snip>
 
K

karthikbalaguru

Hello

I have question about using a signed and unsigned int data types. In a typical
for loop like:

int i;
for(i=0;i<n;i++);

If I used an unsigned int instead of an int, a difference of how many
instructions is achieved? Would it matter if we were considering embedded systems?
It also depends on the type of processor architecture.

Karthik Balaguru
 
C

Cross

karthikbalaguru said:
It also depends on the type of processor architecture.

Karthik Balaguru

Well in this post, I intended to find this dependence out. Embedded systems are
more likely to use 8 or 16 bit processors. Again there are variants of
architecture like SPARC and RISC. So, I wanted to know how the two are different
when architectures change.
 
L

luserXtrog

Well in this post, I intended to find this dependence out. Embedded systems are
more likely to use 8 or 16 bit processors. Again there are variants of
architecture like SPARC and RISC. So, I wanted to know how the two are different
when architectures change.

Get you a cross-compiler and check the assembly generated for the
various architectures of interest.
 
T

Tim Prince

Cross said:
Malcolm McLean wrote:
Tell me more about those where signes ones can be slower.

Array indexing by unsigned int in 32-bit mode usually takes longer than
signed int. There is no corresponding performance issue in 64-bit mode.
 
T

Thomas Matthews

Cross said:
Well in this post, I intended to find this dependence out. Embedded systems are
more likely to use 8 or 16 bit processors. Again there are variants of
architecture like SPARC and RISC. So, I wanted to know how the two are different
when architectures change.

Actually many embedded systems are 32-bit nowadays. Check out the ARM9.

For most embedded systems, this issue is not architecture dependent.
Incrementing is incrementing. A good compiler will translate the
increment operation into the most efficient processor instructions.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
T

Thomas Matthews

Malcolm said:
Signed ints can trap, whilst unsigned ones cannot. So it is easy to think of
an architecture on which signed int is slower, hard to think of one on which
it is faster.
However on all systems I have ever used the timings would be the same.
C itself makes no guarantees, of course.
So where is this information from that only signed ints can trap?
"Traps" or exceptions are dependent on the processor. Some processors
may set a bit in a status word when "overflow" occurs; others may
actually generating an exception (kind of like an interrupt).

In either case, the C language standard leaves the handling of
arithmetic overflow up to the platform / compiler.

The timing is exactly the same for unsigned and signed additions and
increments. Many newer processors can perform increments in less than
one clock cycle and they like to combine it with other functionality,
such as increment pointer then fetch.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
T

Thomas Matthews

Cross said:
Hello

I have question about using a signed and unsigned int data types. In a typical
for loop like:

int i;
for(i=0;i<n;i++);

If I used an unsigned int instead of an int, a difference of how many
instructions is achieved? Would it matter if we were considering embedded systems?

Regards,
Amitav

I highly suggest that you investigate "loop unrolling".

In most embedded systems, the quantity of instructions is negligible.
If time is a concern perform the following:
1. Get rid of any non-data fecthing or non-arithmetic statements.
2. Use boolean arithmetic instead of "if" statements.
3. Unroll the loop in powers of 2.
4. Profile the program to find out where most of the time is spent.
Optimize these areas.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top