Comparing the speed of register and nonregister variables

William Hemsworth 0 Tallied Votes 145 Views Share

Heres a snippet that compares the speed of volatile (nonregister) variables, and register variables and display's the average cycle count for each. This shows you exactly how much using registers speeds up loops and calculations.


I get the following output, but it may vary:

Cycle count for nonregister integer: 8
Cycle count for register integer: 3

#include <iostream>
using namespace std;

// More loops will increase accuracy and precision
const unsigned __int64 loops = 0xFFFFFFF;

// Volatile is effectively the opposite of register
#define nonregister  volatile

// Variables used to store times
unsigned __int64 _pfstart;
unsigned __int64 _pfend;

inline __declspec(naked)
unsigned __int64 GetCycleCount() {
   _asm rdtsc;
   _asm ret;
}

// Begin timer
inline void StartMeasure() {
   _pfstart = GetCycleCount();
}

// End timer
inline void EndMeasure() {
   _pfend = GetCycleCount();
}

// Get time
inline unsigned __int64 GetMeasure() {
   return (_pfend - _pfstart);
}


int main() {

   nonregister unsigned __int64 a = 0;
   register    unsigned __int64 b = 0;

   StartMeasure();
   while (a < loops) ++a;
   EndMeasure();
   cout << "Cycle count for nonregister integer:\t"
        << GetMeasure() / loops
        << '\n';

   StartMeasure();
   while (b < loops) ++b;
   EndMeasure();
   cout << "Cycle count for register integer:\t"
        << GetMeasure() / loops
        << '\n';

   cin.ignore();
   return 0;

}