为什么要为 C++ 应用程序构建 64 位目标? [英] Why should we build 64 bit targets for C++ application?

查看:18
本文介绍了为什么要为 C++ 应用程序构建 64 位目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的一个非常基本的疑问.我不是 IT 或 CS 人,所以请尝试用简单的语言进行解释.现在我问这个问题的原因是因为我们可以在 64 位和 64 位模式下运行 32 位应用程序.32 位操作系统.AFAIK 64 位的数据类型占用的内存量是 32 位应用程序的两倍.此外,64 位应用程序只能在 64 位操作系统上运行.那么为什么要费心构建 64 位应用程序呢?也许这就是为什么 Firefox 仅在 32 位可用???抱歉,如果这个问题与 SO 的标准不符,但我无法控制停止思考相同的问题.谢谢.

This is a very basic doubt that I have. I am not an IT or CS guy so please try to explain in simple language. Now why I am asking that question is that because we can run 32 bit apps in 64 & 32 bit OS's. AFAIK the data types for 64 bit take double amount of memory than 32 bit apps. Also 64 bit apps can only run on 64 bit OS's. Then why take the trouble of building 64 bit applications? Perhaps thats why Firefox is available only in 32 bit??? Sorry if this question is not on par with the standards of SO, but I just cant control stop thinking about the same. Thank You.

更新:不知何故,似乎有些混乱.我并不是要质疑为什么我们需要 64 位架构的机器.我知道 32 位机器只能使用 4GB RAM &64 位机器有更高的限制.我在质疑为什么我们需要构建 64 位应用程序!

UPDATE: Somehow there seems to be a confusion. I did not mean to question why we need a 64 bit architecture machine. I know that 32 bit machines can only use 4GB RAM & 64 bit machines have much higher limit. I was questioning why we need to build 64 bit Applications!

推荐答案

除了上面给出的明显原因(主要是使用超过 2-3GB 的内存"),编译 64 位的原因是 x86-64 有 16 个寄存器,其中 x86-32 有 8 个.由于这些寄存器之一是堆栈指针,并且通常 rBP 是为帧指针"保留的,因此实际有用的寄存器数量分别为 6 和 14.例如,额外的寄存器允许在寄存器中传递更多的参数,以及在函数内的寄存器中保存更多的临时变量.这对代码的实际执行速度有积极影响,因为每次使用内存而不是寄存器时,至少会导致更复杂的指令,并且通常必须使用额外的指令.当没有足够的寄存器时,这反过来会使代码更大.

Aside from the OBVIOUS reasons given above (mainly "using more than 2-3GB of memory"), the reason to compile for 64-bit is that x86-64 has 16 registers, where x86-32 has 8. Since one of those registers is the stackpointer and often rBP is reserved for "framepointer", the actual useful number of registers is 6 and 14 respectively. The additional registers allow, for example, larger number of parameters being passed in registers, and larger number of temporary variables being held in registers within a function. This has a positive effect on the actual execution speed of the code, as every time memory is used instead of a register, AT LEAST it leads to a more complex instruction, and often to an additional instruction having to be used. This in turn makes the code larger when there isn't enough registers.

通常,64 位 x86 代码在不修改实际算法的情况下运行速度提高约 5-15%,并且通常具有 .有时可以更改算法以获得更多[因为例如,您可以拥有一个由电话号码"索引的数组,而不是对电话号码进行散列,然后在散列上建立索引,或者使用 64 位整数值代替32 位的,这意味着一半的操作"速度提高了 2 倍].

Generally, 64-bit x86 code runs some 5-15% faster with no modification to the actual algorithms, and typically has . Sometimes algorithms can be changed to gain much more [because for example you can have an array that is indexed by "telephone number", instead of hashing the phone number and then indexing on the hash, or making use of 64-bit integer values instead of 32-bit ones, meaning a speedup of 2x for "half as many operations"].

如果您需要应用程序的性能,则必须进行基准测试(在多个平台上).例如,在某些情况下,较大的指针意味着缓存会更快地填满,而代码最终会运行得更慢,因为缓存中只有一半的链表条目".

If it's performance you need from the application, you will have to benchmark (on multiple platforms). There are situations where, for example, larger pointers, mean that cache gets full more quickly and the code ends up running slower because "only half as many linked list entries fit in the cache".

简而言之:在大多数情况下,64 位应用比 32 位应用做同样的事情要快.

In short: In most cases, 64-bit apps are faster than 32-bit apps doing the same thing.

这篇关于为什么要为 C++ 应用程序构建 64 位目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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