views:

153

answers:

5

What is the source of the performance advantage 64-bit applications have over 32-bit applications? I'm assuming there is a performance advantage because programs like WinRAR advertise it.

Also, can we get these performance advantages simply by switching to a 64-bit compiler, or are there any changes in code that need to be made?

Answers related to both, unmanaged and managed code, are welcome.

A: 

My understanding is that in most circumstances, there is no performance gain as such, apart from the ability to use more memory. (Although it might give higher performance if you're working with 64-bit data types a lot, for example).

Grant Crofton
+3  A: 

When did they stop lying in ads? Did I miss something? ;-)

The major performance advantage is that in 64bit systems, you can allocate more than 4GB of RAM (actually on most systems that's more 2GB) without swapping. That's a huge speed advantage if you need it.

So 64bit gives an advantage if you have applications that need lots of RAM (image/video/audio processing, world/universe simulations).

On top of that, the 64bit CPUs also have commands that operate on 64bit data types (so you don't need to emulate those with 32bit types). That's also an advantage but it's a) not that big because you the algorithms need to use those types and most don't b) they are still slower then 32bit types (but faster than the emulation).

To give you an idea, here is an old joke from a supercomputer guy: "It takes one day to load the data from disks, then the program takes five minutes to process the data and then we need another day to save the result to disk". Reading data from RAM is between 1000 and 1000000 times faster than an access to disk.

So all in all, for the average user, a 64bit system has little to no advantage.

Aaron Digulla
Your answer brings the second part of my question to the forefront...do we need to make changes in code to optimize for 64-bit? Because if using 64-bit data-types is the only change that needs to be made, we don't actually need to change code for that, because 64-bit compilers automatically convert types like int to 64-bit (because its size is bound to the word-size). But I've also read somewhere that changes to code are required for getting the full benefits of 64-bit...what changes exactly?
TripShock
Most code uses a specific type size instead of plain int. Also, even on 64bit systems, `int` is usually 32 bit because such a lot of code depends on it being 32bit. `long` is another matter but there are compiler options to make it 32bit and `long long` 64bit. That said, what is the advantage of using a 64bit loop counter that is usually well below 100000?
Aaron Digulla
So my conclusion stands: If you have a specific problem (like wading through a lot of data) and you have an algorithm that can make use of huge types, then it matters. Think of fancy real time graphics manipulation for demos or games. Your OS won't get faster *and* it will need a lot more RAM (since every pointer is going to need twice as many bytes).
Aaron Digulla
Swapping or not depends solely on the amount of RAM. When you use more than is physically available it swaps. There are issues around using more than 4GB on 32bit, but swapping is not one of them.
phkahler
In case of WinRar x64, there is a speed gain! Besides, if you ever programmed in assembler, you know that sometimes you need more registers than available, and then you have to use another register or to split out your algorithm. Having more registers, can sometimes improve performance, because the CPU can work more things out in a single cycle. Read here: http://en.wikipedia.org/wiki/CPU_register
John Paul
How much faster is WinRar x64 effectively? i.e. how much of the speed gain is swallowed by slower memory accesses?
Aaron Digulla
Why are memory accesses slower?
TripShock
@TripShock: The number of address lanes on your mainboard doesn't change for a 64bit CPU -> memory accesses need twice as long because memory addresses must be transferred in two chunks.
Aaron Digulla
+8  A: 

The x64 architecture doubles the number of general purpose registers available in the x86 architecture, so that compilers are able to keep more data in (very fast) CPU registers rather than in (relatively slow) RAM.

Ferruccio
Extra registers is huge. Additional addressing modes can help (RIP). And these benefit all code with a simple recompile. x86 was had always been criticized for low register count.
phkahler
A: 

Some great answers here.

As was said, you can access unlimited RAM, which will help performance if you need it.

I would humbly add that if you don't need it, but use it anyway, it will hurt performance.

Never lose sight of one of the oldest laws of science:

Nature Abhors a Vacuum

Mike Dunlavey
You can't access unlimited RAM.. there is a limit: http://en.wikipedia.org/wiki/64-bit#Limitations
John Paul
@John: You know what I meant.
Mike Dunlavey
+2  A: 

In order to take advantage of the 64 bit architecture of the latest CPU's you have to:

  • use a 64 bit CPU and OS
  • develop specifically for 64 bits using a 64 bit api - the implementation has to go all the way down to the most basic code working with the CPU registers (normally written in assembler) to take advantage of the extra registers.
  • develop an application that will really benefit from the extra registers - WinRAR is an application that will take full advantage of the extra registers because it involves a lot of calculus with complex algorithms. If you instead write an application with very simple algorithms, it will not require extra register address space and it will not work faster on 64 bit
  • take also into consideration that when you use a CPU register even if you don't use the the whole address space for a value, it will still take up as much space ( = 64bits).. therefore writing a small application in 64 bit aiming for getting a optimized code will just not work.. that app will take up twice the RAM than if it would be developed under 32 bit and it may be even slower. Programming in 64 bit makes sense for applications using heavy algorithms or that need to allocate huge pieces of memory (4Gb is the limit for a 32bit app).
John Paul
Why would the application take up twice as much RAM?Regarding register space, you can always use the higher and lower pieces of a 64 bit register separately. So judicious use of the registers should naturally solve that issue.
TripShock
Yes, twice the amount of space is basically worst case scenario. But even so if you need to work with values much smaller than 32 bits like values represented on max 16 bits, you still waste half of the register space than if you would be working on a 32 bits CPU. And in general the bigger the registers the bigger the memory waste. Anyway imagining that you write your code in something else than assembler, you are to the mercy of the optimizations made by your compiler.. and there are cases when even an optimal compiler will make your code use double the memory needed in 32 bit architecture
John Paul
@TripShock.. read here for further proof : http://en.wikipedia.org/wiki/64_bit#Pros_and_cons :)
John Paul