C Performance – Is Using 64-bit Integers Faster?

coptimizationperformance

I read in the book "Game Engine Architecture" by Jason Gregory that:

"It’s possible to access data items that are narrower than the width of a machine’s data bus, but it’s typically more costly than accessing items whose widths match that of the data bus. For example, when reading a 16-bit value on a 64-bit machine, a full 64 bits worth of data must still be read from memory. The desired 16-bit field then has to be masked off and possibly shifted into place within the destination register."

So, if I just use long long (64 bit integers), will my code be faster than if I use less bits integers (short, int)? If so, why still using shorts and ints if we can get larger values (when needed) and faster code with just long longs?

Best Answer

In practice, it's not going to matter most of the time.

  1. It won't matter in most programs.
  2. For some programs where it might potentially matter, it might still not matter because there's no significant, measurable performance difference.
  3. For those programs where there is a measurable performance difference, it might still not matter because the program performs adequately anyway.

And so, in order for this to matter, there must be:

  1. A performance difference,
  2. That can be measured,
  3. Where that measurable performance difference materially impacts your application.

"Materially" means that:

  1. The measured performance difference is significant enough to cause one of your performance requirements to fail, and
  2. Using the higher-performing technique, either alone or in concert with other improvement techniques, causes the performance requirement to succeed, as measured by your performance tests.

In short, measure. Profile your code using both techniques, and then use those measurements to determine what to do. It's the only way to be sure.


Incidentally, this is the reason many programming languages use a 32 bit signed integer as the default numeric type. It's usually the best compromise for speed, flexibility, storage space and numeric resolution.

Related Topic