编写软件时 64 位相对于 32 位的好处 [英] Benefits of 64 bit over 32 bit when writing software

查看:26
本文介绍了编写软件时 64 位相对于 32 位的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个像 HelloWorld 这样用 C++ 编写的简单程序,然后我在 32 位和 64 位机器上编译它,我会得到两个不同的二进制文件,但它们是不同的机器代码,只有 32 位二进制文​​件会能够在 32 位或 64 位机器上运行.

在这两种情况下,我都没有任何好处,因为源代码是相同的,而且它们的作用相同.这让我觉得某些为 32 位编写的 Linux 发行版的所有软件包都可以移植到 64 位机器上,而无需做任何更改.那么,我能得到什么?有什么好处吗?

是否有任何 C/C++ 代码示例可以在 64 位中执行而在 32 位中无法执行?

例如,Google Chrome 现在不支持 32 位,但不支持 64 位.可能是什么原因?

解决方案

32 位和 64 位 CPU 之间有太多差异(内存处理、CPU 架构、总线等),无法进入这里,但是最大和最明显的区别是可寻址内存(即指针可以走多远).

以下面的代码为例:

#include int main(int argc, char* argv[]){//这只是为了演示 32 vs. 64int* x = (int*)0xFFFFFFFFFFFFFFFF;int* y = (int*)0x00000000FFFFFFFF;std::cout <<std::hex <<"&x = " <<x<<std::endl <<"&y = " <<y<<std::endl;如果(y == x){std::cout <<对!"<<std::endl;} 别的 {std::cout <<错误的!"<<std::endl;}返回0;}

<块引用>

问:你认为 32 位机器和 64 位机器会打印什么?

A:一个非常不同的结果!

从上面的代码可以看出,如果我希望 x 等于 y 并在 32 位机器上测试它,那么事情就会如我所料我的代码会运行良好,每个人都很开心!但后来我将此代码传递给了一个必须为他们的 64 位机器重新编译的朋友,他们肯定高兴,因为他们看到的都是错误!

我不会深入探讨 32 与 64 的其他差异(如设备和系统驱动程序,或内核模块),因为它超出了本论坛的范围,但希望上面的代码可以说明为什么构建 32-bit 机器,然后为 64 位机器重新编译并不像人们最初想象的那样枯燥无味.

所以更直接地回答你的一些问题:

<块引用>

那么,我能得到什么?有什么好处吗?

这取决于你想做什么.如果您的程序永远达到 32 位 CPU 的限制,那么您不一定会看到为 64 位 CPU 构建的任何好处,并且取决于 CPU 和操作系统,您实际上可能会看到性能下降(就像在 64 位 CPU 上进行 32 位仿真早期的情况一样),但是对于现代内核和操作系统,这对于普通"程序来说基本上不是问题(除了您无法访问超过 4GB 的 RAM.

但是,如果您的项目会消耗大量内存(例如网络浏览器),或者需要对非常大的数字集进行计算(例如 3D 计算),那么您肯定会看到好处事实上,您可以为 64 位版本处理超过 4GB 的 RAM 或更大的分辨率.

这仅取决于您的项目范围以及您愿意支持的架构.

<块引用>

例如,Google Chrome 现在不支持 32 位,但不支持 64 位.可能是什么原因?

仅限 Chrome 团队 可以具体告诉你为什么要这个,但我的猜测与几个原因有关.

首先是 32 位 CPU 正在逐渐消亡,因此停止对垂死架构的支持意味着他们可以专注于改进 64 位架构.

第二个原因可能与内存有关;64 位版本的 Chrome 可以访问超过 4GB 的 RAM(假设系统有更多),因此具有 8GB RAM 的 64 位机器将能够处理更多的浏览器会话,并可能具有更高的响应速度(对单个会话)而不是在 32 位机器上.

此外,Wiki 有一个非常好的页面,详细介绍了 32 位到 64 位转换和各种注意事项,如果您有兴趣深入了解差异.

希望能帮到你.

If I have a simple program written in C++ like HelloWorld, and then I compile it in a machine of 32 bit and 64 bit, I get two different binaries doing the same but they are different machine code and only 32 bit binary will be able to run on 32 or 64 bit machine.

In both case I don't have any benefits because the source code is the same and they do the same. This makes me to think the all software packages of some Linux distro written for 32 bit could be ported to 64 bit machine without to do anything change. Then, what do I get ? Any benefits?

Is there any example of code in C/C++ that I can do some in 64-bit that I can't do in 32-bit ?

For example, Google Chrome right now is unsupported in 32 bit, but not in 64 bit. Which could be the reason?

解决方案

There are too many differences (memory handling, CPU architecture, bus, etc.) between a 32-bit and 64-bit CPU to get into here, but the biggest and most obvious difference is addressable memory (i.e. how far your pointer can go).

Take the following code for example:

#include <iostream>

int main(int argc, char* argv[])
{
    // this is just to demonstrate 32 vs. 64
    int* x = (int*)0xFFFFFFFFFFFFFFFF;
    int* y = (int*)0x00000000FFFFFFFF;
    std::cout << std::hex << 
        "&x = " << x << std::endl <<
        "&y = " << y << std::endl;
    if (y == x) {
        std::cout << "RIGHT!" << std::endl;
    } else {
        std::cout << "WRONG!" << std::endl;
    }
    return 0;
}

Q: What do you think will be printed on a 32-bit machine vs. a 64-bit machine?

A: A very different result!

As you can see from the above code, if I expect x to equal y and test this on a 32-bit machine, then things will go as I expect and my code will run fine and everyone's happy! But then I pass this code to a friend who has to recompile for their 64-bit machine and they are most certainly not happy since all they see is WRONG!

I won't go deep into the other differences of 32 vs. 64 (like device and system drivers, or kernel modules) since it's beyond the scope of this forum, but hopefully the above code can illustrate why building for a 32-bit machine and then re-compiling for a 64-bit machine isn't as cut and dry as one would initially think.

So to answer some of your questions more directly:

Then, what do I get ? Any benefits?

It depends on what you're trying to do. If you have a program that will never reach the limits of 32-bit CPU's then you won't necessarily see any benefits of building for a 64-bit CPU, and depending on the CPU and OS, you might actually see a degradation in performance (as was the case in the early days of 32-bit emulation on 64-bit CPU's), but with modern cores and OS's, this is largely a non-issue for the "average" program (save the fact that you can't access more than 4GB of RAM).

However, if you have a project that would consume massive amounts of memory (like a web-browser), or need to do calculations for very large sets of numbers (like 3D calculations), then you will most certainly see a benefit in the fact that you can address more than 4GB of RAM or larger resolution numbers for your 64-bit build.

It just depends on the scope of your project and what architectures you're willing to support.

For example, Google Chrome right now is unsupported in 32 bit, but not in 64 bit. Which could be the reason?

Only the Chrome team can specifically tell you why to this one, but my guess has to do with a couple of reasons.

First is the fact that 32-bit CPU's are largely dying out and thus killing off support for a dying architecture means they can focus on improving the 64-bit architecture.

The second reason probably has to do with memory; the 64-bit version of Chrome can access more than 4GB of RAM (assuming the system has more than that) and thus a 64-bit machine with 8GB of RAM would be able to handle more browser sessions and potentially be more responsive (to the individual sessions) than on a 32-bit machine.

Additionally, Wiki has a pretty good page that details more of the 32-bit to 64-bit transition and the various considerations, should you be interested in diving in more on the differences.

Hope that can help.

这篇关于编写软件时 64 位相对于 32 位的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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