Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference in term of performance between "unsigned int" and "int" on the IPhone?

Or to reformulate the question: is there a performance penalty in using unsigned values?

And in general: what is the most performant type (16bit signed?, 32bit signed? etc.) on the IPhone ARM processor?

like image 268
Ariel Malka Avatar asked Jan 04 '09 13:01

Ariel Malka


People also ask

Is unsigned int faster?

On most processors, there are instructions for both signed and unsigned arithmetic, so the difference between using signed and unsigned integers comes down to which one the compiler uses. If any of the two is faster, it's completely processor specific, and most likely the difference is miniscule, if it exists at all.

Should you use unsigned ints?

When no negative numbers are required, unsigned integers are well-suited for networking and systems with little memory, because unsigned integers can store more positive numbers without taking up extra memory. New programmers sometimes get signed and unsigned mixed up.

What is the difference between unsigned int and int?

An int is signed by default, meaning it can represent both positive and negative values. An unsigned is an integer that can never be negative.

Is unsigned int same as unsigned?

There is no difference. unsigned and unsigned int are both synonyms for the same type (the unsigned version of the int type).


1 Answers

It always depends:

For loops signed integers as counters and limits are a tad faster because in C the compiler is free to assume that overflow never happends.

Consider this: You have a loop with an unsigned loop counter like this:

void function (unsigned int first, unsigned int last)
{
  unsigned int i;
  for (i=first; i!=last; i++)
  {
     // do something here...
  }
}

In this loop the compiler must make sure that the loop even terminates if first is greater than last because I will wrap from UINT_MAX to 0 on overflow (just to name one example - there are other cases as well). This removes the opportunity for some loop optimizations. With signed loop counters the compiler assumes that wrap-around does not occur and may generate better code.

For integer division otoh unsigned integers are a tad faster on the ARM. The ARM does not has a hardware divide unit, so division is done in software, and is always done on unsigned values. You'll save some cycles for the extra-code required to turn a signed division into an unsigned division.

For all other things like arithmetic, logic, load and write to memory the choice of sign-ness will not make any difference.


Regarding the data-size: As Rune pointed out they are more or less of equal speed with 32 bit types beeing the fastest. Bytes and words sometimes need to be adjusted after processing as they reside in a 32 bit register and the upper (unused) bits need to be sign or zero extended.

However, the ARM CPU's have a relative small data-cache and are often connected to relative slow memory. If you're able to utilize the cache more efficient by choosing smaller data-types the code may executes faster even if the theoretical cycle-count goes up.

You have to experiment here.

like image 79
Nils Pipenbrinck Avatar answered Sep 19 '22 06:09

Nils Pipenbrinck