473,494 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

int versus unsigned int - optimizing for speed

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
Dec 12 '06 #1
4 11936
RS <rs***************@comcast.netwrote:
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
Dec 12 '06 #2
On Dec 12, 4:12 am, RS <rsina_no.ssppa...@comcast.netwrote:
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.

--
Erik Wikström

Dec 12 '06 #3
RS wrote:
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.

--
Salu2
Dec 12 '06 #4
RS <rs***************@comcast.netwrote:
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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Dec 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2857
by: A Future Computer Scientist | last post by:
A question: Is it really important to think about optimizing the native code or optimizing it for P Code? Or does the code you write make a difference?
30
13689
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with...
4
2500
by: J. Campbell | last post by:
From reading this forum, it is my understanding that C++ doesn't require the compiler to keep code that does not manifest itself in any way to the user. For example, in the following: { for(int...
3
3307
by: PWalker | last post by:
Hi, I have written code that I would like to optimize. I need to push it to the limit interms of speed as the accuracy of results are proportional to runtime. First off, would anyone know any...
9
4501
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our...
54
5730
by: Matt | last post by:
How do we define systems programs? when we say systems programming, does it necessary mean that the programs we write need to interact with hardware directly? For example, OS, compiler, kernel,...
20
5314
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include...
24
3137
by: Richard G. Riley | last post by:
Without resorting to asm chunks I'm working on a few small routines which manipulate bitmasks. I'm looking for any guidance on writing C in a manner which tilts the compilers hand in, if possible,...
9
4069
by: anon.asdf | last post by:
In terms of efficieny: Is it better to use multiple putchar()'s after one another as one gets to new char's OR is it better to collect the characters to a char-array first, and then use...
0
7119
marktang
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,...
1
6873
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...
0
7367
tracyyun
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...
0
5453
agi2029
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,...
1
4889
isladogs
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...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1400
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 ...
1
644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.