473,779 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
+ 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 11963
RS <rs************ ***@comcast.net wrote:
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.net wrote:
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(wil l 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.net wrote:
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
2877
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
13756
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 this? I would think that the library function std::copy would perform optimally, as it is a library function, and therefore the writers of this function would know best how to optimize it ... but some tests seem to indicate that my pointer copy...
4
2534
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 i = 0; i < 10; ++i){ std::cout << i << std::endl; for(int j = 0; j < 0x7fffffff; ++j){} } }
3
3334
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 resources that explains how to optimize code i.e. give some rules on c++ optimization? e.g. using memcpy to copy an array (which i have done). Also, what is the best sorting algorithm out there for sorting an array of of size 100 or less? I have...
9
4524
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 purposes. I have to admit that, although I'm very familiar with Python, I'm complete Perl noob (and I hope to stay one) which is reflected in my questions. I know that the web offers a lot of resources on Python/Perl differences. But I couldn't find a...
54
5863
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, drivers, network protocols, etc...? Couple years ago, yes, I understand this is definitely true. However, as the software applications become more and more complicated, some people try to argue that. Some people argue the definition of systems...
20
5365
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 <stdlib.h>
24
3168
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, a compiler/underlying processor independant way : althought to be fair I cant see this stuff on anything other than x86, but who knows. I found some ok info here: http://www.eventhelix.com/RealtimeMantra/Basics/OptimizingCAndCPPCode.htm...
9
4100
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 puts() to print to screen ????
0
9636
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10074
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7485
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6724
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4037
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 we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.