简体   繁体   English

对不同数据类型的操作

[英]Operation on different data types

Considering the basic data types like char, int, float, double etc..in any standard language C/C++, Java etc 在任何标准语言C / C ++,Java等中考虑基本数据类型,如char,int,float,double等

Is there anything like.."operating on integers are faster than operating on characters".. by operating I mean assignment, arithmetic op/ comparison etc. Are data types slower than one another? 是否有类似的东西......“对整数进行操作比对字符进行操作更快”..通过操作我的意思是赋值,算术运算/比较等等。数据类型是否比另一个慢?

For almost anything you're doing this has almost no effect, but purely for informational purposes , it is usually fastest to work with data types whose size is machine word size (ie 32 bits on x86 and 64-bits on amd64). 对于你正在做的几乎任何事情,这几乎没有任何影响,但纯粹出于信息目的 ,使用大小为机器字大小的数据类型(即x86上的32位和amd64上的64位)通常最快。 Additionally, SSE/MMX instructions give you benefits as well if you can group these and work on them at the same time 此外,如果您可以将这些指令分组并同时处理它们,SSE / MMX指令也会给您带来好处

Rules for this are a bit like rules for English spelling and/or grammar. 这个规则有点像英语拼写和/或语法规则。 The rules are broken at least as often as they're followed. 这些规则至少在遵循规则的情况下被打破。

Just for example, for years "everybody has known" that floating point operations are slower than integers, especially for more complex operations like multiply and divide. 例如,多年来“每个人都知道”浮点运算比整数慢,特别是对于更复杂的运算,如乘法和除法。 In reality, some processors do some integer operations (especially multiplication and division) by converting the operands to floating point, doing the operation in floating point, then converting the result back to an integer. 实际上,一些处理器通过将操作数转换为浮点,在浮点上执行操作,然后将结果转换回整数来执行一些整数运算(尤其是乘法和除法)。 As you'd expect from that, the floating point operation is actually faster (though only a little bit). 正如您所期望的那样,浮点运算实际上更快(尽管只是一点点)。

Most of the time, however, it doesn't matter much -- in a lot of cases, it's quite reasonable to think of the operations on the processor itself as free, and concern yourself primarily with optimizing your use of bandwidth to memory. 然而,大多数时候,它并不重要 - 在很多情况下,将处理器本身的操作视为免费是非常合理的,并且主要关注优化对内存的带宽使用。 Of course, doing that well is often even harder... 当然,做得好往往更难......

yes , some data types are definitely slower than others. 是的,一些数据类型肯定比其他数据类型慢。 For example , floats are more complicated than int's and thus may incur additional penalties when doing divides and multiplies. 例如,浮点数比int更复杂,因此在进行除法和乘法运算时可能会产生额外的惩罚。 It all depends on how your hardware is setup and what kind of instructions it supports. 这完全取决于您的硬件设置方式以及它支持的指令类型。

Data types which is longer than the machine word size will also be slower because it takes more cycles to perform operations. 长于机器字大小的数据类型也会变慢,因为执行操作需要更多周期。

depending on what you do, the difference can be quite large, especially when working with floats versus double versus long double. 取决于你做什么,差异可能非常大,特别是在使用花车而不是双倍与长双倍时。

In modern processors it comes down to simd instructions, which have certain length, most commonly 128 bit. 在现代处理器中,它归结为simd指令,它具有一定的长度,最常见的是128位。 so four float versus two double numbers. 四个浮动对两个双数。

However some processors only have 32 bit simd instructions(PPC) and GPU hardware has a factor of eight performance difference between float and double. 但是,有些处理器只有32位simd指令(PPC),而GPU硬件在float和double之间的性能差异为8。

when you add trigonometric , exponential, and square root functions into the mix, float numbers are going to have better performance overall given number of factors. 当您将三角函数,指数函数和平方根函数添加到混合中时,浮点数将在给定数量的因素下具有更好的性能。

Almost all of the answers on this page are mostly right. 几乎所有这个页面上的答案都是正确的。 The answer, however, varies wildly depending upon your hardware, language, compiler, and VM (in managed languages like Java). 但是,答案会有很大差异,具体取决于您的硬件,语言,编译器和VM(在Java等托管语言中)。 On most CPUs, your best performance will be to do the operations on a data type that fits the native operand size of your CPU. 在大多数CPU上,最佳性能是对适合CPU本机操作数大小的数据类型执行操作。 In some cases, some compilers will optimize this for you, however. 但是,在某些情况下,某些编译器会为您优化。

On most modern desktop CPUs the difference between floating point and integer operations has become pretty trivial. 在大多数现代桌面CPU上,浮点和整数运算之间的差异变得非常微不足道。 However, on older hardware and a lot of embedded systems the difference in all of these factors can still be really, really big. 但是,在较旧的硬件和许多嵌入式系统上,所有这些因素的差异仍然非常大。

The important thing is to know the specifics of your target architecture and your tools. 重要的是要了解目标架构和工具的细节。

This answer relates to the Java case (only). 这个答案与Java案例(仅)有关。

The literal answer is that the relative speed of the primitive types and operators depends on your processor hardware and your JVM implementation. 字面上的答案是原始类型和运算符的相对速度取决于您的处理器硬件和JVM实现。

But a better answer is that it usually doesn't make aa lot of difference to performance what representations you use. 但更好的答案是,它通常不会对您使用的表现形式产生很大的影响。 Indeed, any clever data type optimizations you do to make your code run fast on your current machine / JVM may turn out to be anti-optimizations on a different machine / JVM combination. 实际上,为了使代码在当前机器/ JVM上快速运行,您所做的任何聪明的数据类型优化都可能成为不同机器/ JVM组合的反优化。

In general, it is better to pick a data type that represents your data in a correct and natural way, and leave it to the compiler to sort out the details. 通常,最好以正确和自然的方式选择代表数据的数据类型,并将其留给编译器来整理细节。 However, if you are creating large arrays of a primitive type, it is worth knowing that Java uses compact representations for arrays of boolean, byte and short. 但是,如果要创建基本类型的大型数组,则值得知道Java对布尔,字节和短数组使用紧凑表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM