cpu - In C++, is it better to cap a value using std::min or an if branch? -
A very common pattern in programming is to caps a value at the maximum after any kind of updates. What I want to know, is that if there is a difference between the two pieces of code, and if there should be a favorite:
value + = increment; Value = std :: min (value, value max); versus
value + = increments; If (value> Price Max) value = valueMax; My thinking is that it comes down to the fact that if there are instructions for taking two values of CPU and minimum output, then call std :: min in this instruction. Want results and avoid an unnecessary branch. If not, the second edition avoids an unnecessary assignment when the value I am not very good with such things, but I am sure that old school assembly hackers who know this. I ask them: Which is better?
Modern compilers are smart enough to generate the same code in both cases. For example, 32-bit GCC generates:
ADL% esi,% edi cmpl% edx,% edi movl% edi,% eax cmovgl% edx,% eax 64-bit clang: % 1 = NSW iOS% increments,% value% 2 = ICMP SGT i32% 1,% value Max% value = IIIA% 2, i32 Choose% value max, i32% 1
Comments
Post a Comment