Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiency of different integer sizes on a 64-bit CPU

In a 64-bit CPU, if the int is 32 bits whereas the long is 64 bits, would the long be more efficient than the int?

like image 785
rubbishbin01 rubbishbin01 Avatar asked Sep 16 '12 13:09

rubbishbin01 rubbishbin01


People also ask

How much RAM can a 64-bit processor theoretically?

The theoretical memory limit that a 64-bit computer can address is about 16 exabytes (16 billion gigabytes), Windows XP x64 is currently limited to 128 GB of physical memory and 8 TB of virtual memory.

How many bytes can a computer with a 64-bit data bus transfer at one time?

64 bit CPU can access 8 bytes at one time It is important to note that while 128-bit RAM is very different from 3-bit RAM, 128-bit RAM occupies a finite space. In a 2+4 2-way bus the number of processes is usually more than three processes.

How many characters can a 64-bit processor process at one time?

A 64-bit processor can actually process much more than twice the data that a 32-bit processor can handle. In fact, a 64-bit processor can theoretically process up to 18,446,744,073,709,551,616 bytes, or 16 exabytes (EB) at one time.


1 Answers

The main problem with your question is that you did not define "efficient". There are several possible efficiency related differences.

Of course if you need to use 64 bits, then there's no question. But sometimes you could use 32 bits and you wonder if it would be better to use 64 bits instead.

Data Size Efficiency

Using 32 bits will use less memory. This is more efficient especially if you use a lot of them. Not only it's more efficient in the sense that you may not get to swap out, but also in the sense that you'll have fewer cache misses. If you use just a few then the efficiency difference is irrelevant.

Code Size Efficiency

This is heavily dependent on the architecture. Some architectures will need longer instructions to manipulate 32 bit values, others will need longer instructions to manipulate 64 bits values and others will make no difference. On the intel processors, for example, 32 bits is the default operand size even for 64 bits code. Smaller code may have a little advantage both in cache behavior and in pipeline usage. But it is dependent on the architecture which operand size will use smaller code.

Execution Speed Efficiency

In general there should be no difference beyond the one implied by code size. Once the instruction has been decoded the timing for mere execution are generally identical. However, once again, this is in fact architecture specific. There are architectures that do not have native 32 bit arithmetic, for example.

My suggestion:

If it's just some local variables or data in small structures that you do not allocate in huge quantities, use int and do it in a way that does not assume a size, so that a new version of the compiler or a different compiler that use a different size for int will still work.

However if you have huge arrays or matrixes, then use the smallest type you can use and make sure its size is explicit.

like image 119
Analog File Avatar answered Sep 25 '22 06:09

Analog File