# micro-optimization question

Discussion in 'C Programming' started by Cross, Jun 23, 2009.

1. ### CrossGuest

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

Cross, Jun 23, 2009

2. ### Ben PfaffGuest

Cross <> writes:

> 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.
--
Ben Pfaff
http://benpfaff.org

Ben Pfaff, Jun 23, 2009

3. ### Jean-ChristopheGuest

On 23 juin, 23:26, Cross <> wrote:

> 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
}

Jean-Christophe, Jun 24, 2009
4. ### Peter NilssonGuest

Jean-Christophe <> wrote:
> ...
> // This will *NOT* work ---------------
>
> #define MAX 1000
>
> unsigned int i;
>
>  for( i = -1; ++i < MAX; )
>  {
>    // some code
>  }

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

<snip>
> // 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.

--
Peter

Peter Nilsson, Jun 24, 2009
5. ### user923005Guest

On Jun 23, 3:26 pm, Cross <> wrote:
> 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.

user923005, Jun 24, 2009
6. ### TonyGuest

Cross wrote:
> 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.

Tony, Jun 24, 2009

Cross wrote:
> 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.

--

8. ### CrossGuest

Tony wrote:
> Cross wrote:
>> 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'.

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.

Cross, Jun 24, 2009
9. ### CrossGuest

Peter Nilsson wrote:
<snip>
>
> 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.
>
> --
> Peter

Thanks Peter for the clarification; but I already got his point.

Cross, Jun 24, 2009
10. ### CrossGuest

Jean-Christophe wrote:

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

Cross, Jun 24, 2009
11. ### Nick KeighleyGuest

Subject: "micro-optimization question"

On 24 June, 07:17, Cross <> wrote:
> Tony wrote:
> > Cross wrote:

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

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

Nick Keighley, Jun 25, 2009
12. ### CrossGuest

Nick Keighley wrote:

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

Thanks. Now I have.

Cross, Jun 26, 2009
13. ### karthikbalaguruGuest

On Jun 24, 3:26 am, Cross <> wrote:
> 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

karthikbalaguru, Jun 26, 2009
14. ### CrossGuest

karthikbalaguru wrote:
> 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.

Cross, Jun 26, 2009
15. ### luserXtrogGuest

On Jun 26, 4:12 pm, Cross <> wrote:
> karthikbalaguru wrote:
> > 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.

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

--
lxt

luserXtrog, Jun 27, 2009
16. ### Tim PrinceGuest

Cross wrote:
> Malcolm McLean wrote:

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

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

Tim Prince, Jun 28, 2009
17. ### Tim PrinceGuest

Joe Wright wrote:
> Tim Prince wrote:

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

>
> Where does that come from?
>

practice apparently more often indulged in by C++ programmers,
sometimes discussed as a C vulnerability than as a performance question
(is either of those topical here?):

https://www.securecoding.cert.org/c...p;jsessionid=C0133C003E1DC5DFFA83BF496534B139

Tim Prince, Jun 28, 2009
18. ### Thomas MatthewsGuest

Cross wrote:
> karthikbalaguru wrote:
>> 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.

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

Thomas Matthews, Jun 28, 2009
19. ### Thomas MatthewsGuest

Malcolm McLean wrote:
> "karthikbalaguru" <> wrote in message
>>> 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.
>>

> 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

Thomas Matthews, Jun 28, 2009
20. ### Thomas MatthewsGuest

Cross wrote:
> 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

Thomas Matthews, Jun 28, 2009