I have this peace of code and I would like to make it as fast as possible.
I am not an experienced c++ developer so I would love to know if you guys come up with some really good reinplementation of this algorithm since I removed all the assignments thinking it was a good thing to do...
And now I don't really know if that was the best thing to do.
So, what is faster?
for(register uint pPos = 0; pPos < size; pPos++) {
img->setPixel(pPos % dst_w, pPos / dst_w,
buffer32[
sf * (
(pPos / dst_w * src_w) +
(pPos % dst_w)
)
]);
}
or
for(register uint pPos = 0, x = 0, y = 0; pPos < size; pPos++) {
x = pPos % dst_w;
y = pPos / dst_w;
img->setPixel(x, y,
buffer32[
sf * (
(y * src_w) + x
)
]);
}
Side note: I really thought it was a good thing to ask, I don't understand the down votes.
Also thank you all for the comments, learned a lot.
The lowest level, simple assignments are faster than multiplications or additions.
Some processors have instructions that can perform multiplication or addition and assignment in one instruction.
Stepping back a level, assignments and arithmetic operations between registers is faster than performing the operations with memory. Accessing cache is usually faster than on-chip memory. The further the data is from the processor core, the slower the access. Memory outside the chip would be slower to access than memory on the same piece of silicon containing the processor.
The Implications of Faster
So we know which operations are faster. The often overlooked questions are:
Let us take a hypothetical processor:
So the "savings" between an addition operation and an assignment is 30 nanoseconds. The savings between a multiply operation and addition is 50 nanoseconds. Remember that Users cannot distinguish anything smaller than 1E-2 seconds. So, how many iterations will it take to make 50 nanoseconds noticeable?
With modern processors, a plethora of iterations must be performed in order to gain significant time from one of these instruction level changes. So the return on investment (the time it takes you to optimize these instructions) is not worthwhile. The ROI is only high when the program performance impacts sales or requirements (such as critical systems).