Wednesday, 3 May 2017

c++ - Comparison Execution Speed



I wrote a simple for loop like this.



for(int i = 0; i != 100; i++) 



then someone gave an opinion that it should be



for(int i = 0; i < 100; i++) 


When this turns into assembly mine should turn into Jump on Equality JNE and the latter should be Jump if Greater JG. Or the compiler will do something completely different and it will both turn into the same thing.



Anyway which one is more correct, i find the first one more logically correct because it is known that i WILL pass 100, the greater than check seems logically reduntant. Also are JNE and JG as fast as each other?


Answer




For this case, there's no practical difference between using < and !=. Many iterators, however, don't define < comparisons, only != or ==, in which case those are your only real choice.



jne and jg will normally be the same speed. You might observe differences in speed between the two, but if so they're likely to stem from things like cache usage, not the instructions themselves.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...