This is in reference to C++ and using the Visual Studio compilers.
Is there any difference in speed when reading/writing (to RAM) and doing mathematical operations on different types of variables such as bool, short int, int, float, and doubles?
From what I understand so far, mathematical operations with doubles takes much longer (I am talking about 32 bit processors, I know little about 64 bit processors) than, say, operations with floats.
How then do operations (reading/writing to ram and elementary math) with float and int compare? How about int and short int, or even differences between signed and unsigned versions of each of the types? Is there any one data type that would be most efficient to work with as low number counters?
Thanks,
-Faken
There are two different questions here: speed when reading/writing, and arithmetic performance. Those are orthogonal. When reading or writing a large array, of course, the speed depends on the amount of bytes read as O(N), so using
shortoverint(considering VC++) would slash the time by ~1/2.For arithmetic, once the operands are in registers, the size of the type doesn’t matter so much. IIRC, between types in the same category, it is actually the same (so
shortisn’t any faster or slower thanint). Using 64-bit integer types on a 32-bit platform will have a penalty, naturally, since there’s no single instruction to handle that. Floating-point types, on the other hand, are simply slower than all integral types, even thoughsizeof(float)==sizeof(int)on VC++. But, again, operations onfloataren’t any faster than operations ondouble; this is assuming default FPU settings, which promote all operands to 80-bit extended floats – this can be disabled to squeeze out a bit more out of usingfloats, IIRC.The above is VC++ and x86 specific, as requested by the question. Other platform, and especially other architecture, can differ radically.
The best one data type that is most efficient to work with as number counter (low or not) is
int– usually regardless of the architecture and implementation (as the Standard recommends it to be the preferred word size of the platform).