Reputation: 211
If I perform a computation written in C, something like matrix-matrix addition or matrix-matrix multiplication, where the work is done in a for
loop, with the same number and type of arithmetic operations happening on each iteration, could the specific values of the input data impact the speed of the computation? For example, if the matrix elements are 32-bit integers with values between 0 and 127, so that their representations vary only in one out of each four corresponding bytes, would that run faster than if the values varied between 0 and INT_MAX
(supposing no undefined behavior occurs)?
If I had to guess I would say no, because whether or not the value is small or very large, since it is defined as a 32-bit integer it will be masked to fit in the same amount of memory space so the small and large values will end up having the same amount of bytes in memory. Am I thinking about this correctly? Is it different for single/double precision floating point numbers?
Upvotes: 4
Views: 268
Reputation: 206
The data contained within a variable will not affect the performance it is just a storage space. If the data in this storage space has to be manipulated in some fashion you could encounter some performance issues. Here are a couple of things to consider that could affect performance related to this.
Depending on the hardware architecture of the CPU (i.e. HW FPU vs SW floating point implementation, pipelined vs not pipelined etc...) performing a CPU intensive mathematical operation (i.e. division, square root, etc...) on different data sets could affect performance.
Another scenario that could affect performance is where that variable resides in memory. Is the variable byte aligned in a way that it will only take one instruction cycle to access the data? Or is it misaligned so it takes an extra instruction cycle to retrieve all the data. Often in these cases the compiler will have special keywords or even pragma's that can force variables to be aligned for optimization.
Best Regards!
Upvotes: 1