int versus unsigned int - optimizing for speed

R

RS

Hi all,

I was told that using unsigned int instead of int can speed up the code.
Is this true? If so, why?

Are there are any other rules one should follow to optimize the code for
speed (i.e. using float instead of double, short instead of int,
unsigned short instead of short, etc.)?

Thanks,
RS
 
S

Steve Pope

RS said:
I was told that using unsigned int instead of int can speed up the
code. Is this true?

This sounds exceedingly unlikely to me. But, if it's important to
you, use your profiler and see if you can detect any difference.

Steve
 
E

eriwik

Hi all,

I was told that using unsigned int instead of int can speed up the code.
Is this true? If so, why?

I can only come up with one instance, and it have nothing to do with
the type as such. If you know that a value should never be negative
(like the index in an array) you can use an unsigned and then you don't
have to check for negative values when evaluating input to functions.
Perhaps if you are working on an embedded processor of some sort there
might be a difference between signed and unsigned, but on most
general-purpose machines there is not.
Are there are any other rules one should follow to optimize the code for
speed (i.e. using float instead of double, short instead of int,
unsigned short instead of short, etc.)?

A good rule: use int for integers and double for reals. If you need
lots of reals (millions) you might want to consider float to reduce the
memory, the same goes for ints and shorts. Performance-wise most
computers will work about as fast on floats as on doubles, ints are
always a good bet as the fastest integer type.

If you really need the fastest type to work with and have a C99
compliant compiler/library include the file <stdint.h> (will be in the
next C++ standard as <cstdint>) and use the 'int_fastX_t' (or
'uint_fastX_t' for unsigned) where X is the least number of bits you
need, i.e. int_fast32_t will be the fastest integer type of at least 32
bits. On most machines this is the normal int.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

RS said:
I was told that using unsigned int instead of int can speed up the code.
Is this true? If so, why?

For example:
a= (b + c) / 2;
If a, b and c are unsigned int, the compiler can easily convert the division
by two to a bit shift, which is faster in general. If they are signed this
optimization is not always possible or need more instructions.

In general, the more information the compiler has, better code it can
generate. If the numbers you use are always positive, making it unsigned is
the way to make that information available to the compiler.
 
M

Marcus Kwok

RS said:
I was told that using unsigned int instead of int can speed up the code.
Is this true? If so, why?

It may or may not be true, depending on your platform, compiler, and
compiler options. The only true way to see if something speeds up your
code is to profile.
Are there are any other rules one should follow to optimize the code for
speed (i.e. using float instead of double, short instead of int,
unsigned short instead of short, etc.)?

These may or may not speed up your code. For example, suppose on your
architecture that a machine word is 4 bytes, int is 4 bytes, short is 2
bytes, and the underlying architecture always retrieves memory in
word-sized chunks. If you use int, then it can grab the int in one
instruction, whereas if you use short then it has to retrieve the word
and mask/shift it to get the short. Again, you need to profile to see
what really works.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top