Velocity Reviews > micro-optimization question

# micro-optimization question

Cross
Guest
Posts: n/a

 06-23-2009
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

Ben Pfaff
Guest
Posts: n/a

 06-23-2009
Cross <(E-Mail Removed)> 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

Jean-Christophe
Guest
Posts: n/a

 06-23-2009
On 23 juin, 23:26, Cross <(E-Mail Removed)> 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
}

Peter Nilsson
Guest
Posts: n/a

 06-24-2009
Jean-Christophe <(E-Mail Removed)> 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

user923005
Guest
Posts: n/a

 06-24-2009
On Jun 23, 3:26*pm, Cross <(E-Mail Removed)> 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.

Tony
Guest
Posts: n/a

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

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.

Thad Smith
Guest
Posts: n/a

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

--
Thad

Cross
Guest
Posts: n/a

 06-24-2009
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
Guest
Posts: n/a

 06-24-2009
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
Guest
Posts: n/a

 06-24-2009
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.

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM eddiec MCSE 6 05-20-2004 06:59 AM Wayne Cisco 0 03-02-2004 07:57 PM sean ASP .Net 1 10-20-2003 12:18 PM

Advertisments