“unsigned int”和“unsigned int”之间的性能是否存在差异?和“int”在iPhone上? [英] Is there a difference in term of performance between "unsigned int" and "int" on the IPhone?

查看:140
本文介绍了“unsigned int”和“unsigned int”之间的性能是否存在差异?和“int”在iPhone上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或者重新提出问题:使用无符号值是否存在性能损失?

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

一般来说:性能最高的是什么类型(16位签名?,32位)在iPhone ARM处理器上签名?等等?

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

推荐答案

它总是取决于:

对于循环有符号整数作为计数器和限制有点快,因为在C中编译器可以自由地假设溢出永远不会发生。

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

在这个循环中,如果first大于last,编译器必须确保循环终止,因为我将从UINT_MAX换行到0溢出(仅举一个例子 - 还有其他情况)。这消除了一些循环优化的机会。对于带符号的循环计数器,编译器假定不会发生回绕并且可能生成更好的代码。

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.

对于整数除法otoh无符号整数是在ARM上更快。 ARM没有硬件除法单元,因此除法是在软件中完成的,并且总是在无符号值上完成。您可以为将有符号的除法转换为无符号除法所需的额外代码保存一些周期。

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.

关于数据 - 尺寸:正如Rune指出的那样,它们或多或少具有相同的速度,32位类型的速度最快。有时需要在处理后调整字节和字,因为它们驻留在32位寄存器中,而上(未使用)位需要符号或零扩展。

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.

但是,ARM CPU具有相对较小的数据缓存,并且通常连接到相对较慢的内存。如果您能够通过选择较小的数据类型来更有效地利用缓存,即使理论周期数增加,代码也可以更快地执行。

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.

你必须在这里试验。

这篇关于“unsigned int”和“unsigned int”之间的性能是否存在差异?和“int”在iPhone上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆