The last time I answered such a question, I just wrote a "measure" and filled in the periods until SO agreed.
This answer was omitted 3 times in a few minutes and was deleted (along with at least one other answer to the question) using the SO moderator.
However, there is no alternative to measurement.
So this is the only possible answer.
And in order to discuss this in detail and in detail, the answer is not only omitted and deleted, you need to keep in mind that you measure only that one set of measurements will not necessarily tell you anything at all, but only a specific result. Of course, this may sound patronizing, talking about such obvious things. So, well, let it be so: just measure.
Or, perhaps, I should mention that most processors have special instructions for comparing with zero, and yet this does not allow anyone to do anything about the performance of your code snippets?
Well, I think I stop there. Remember: measurement. And don't optimize prematurely!
EDIT : Amendment with comments referenced in @MooingDuck's comment.
Question:
Doing comparisons in C ++ with int is x >= 0 more efficient than x > -1 ?
What is wrong with the question
Donald Knuth, author of the classic three-volume work, The Art of Computer Programming, once wrote [1],
“We must forget about little efficiency, say, about 97% of the time: premature optimization is the root of all evil”
How effective x >= 0 compared to x > -1 , often does not matter. That is, this is most likely the wrong thing to focus on.
How clearly he expresses what you want to say is much more important. Your time and time for others supporting this code is usually much more important than program runtime. Focus on how well the code interacts with other programmers, i.e. Focuses on clarity.
Why is the focus of the question wrong
Clarity affects the likelihood of correctness. Any code can be made arbitrarily quickly if it does not have to be correct. Therefore, correctness is important, and this means that clarity is very important - much more important than shaving the nano-seconds of the & hellip;
And both expressions are not equivalent. clarity and rights. to their chance to be true.
If x is a signed integer, then x >= 0 means exactly the same as x > -1 . But if x is an unsigned integer, for example. of unsigned type, then x > -1 means x > static_cast<unsigned>(-1) (via implicit promotion ), which in turn means x > std::numeric_limits<unsigned>::max() . This is probably not what the programmer wanted to express!
Another reason why the focus is wrong (on micro-efficiency, although it should be on clarity) is that the main effect on efficiency does not usually come from the timings of individual operations (except, in some cases, from dynamic allocation and from even slower disk and network operations), but from algorithmic efficiency . For example, write & hellip;
string s = ""; for( int i = 0; i < n; ++i ) { s = s + "-"; }
quite inefficient, because it uses time proportional to the square of n , O ( n 2 ), quadratic time .
But instead write & hellip;
string s = ""; for( int i = 0; i < n; ++i ) { s += "-"; }
reduces the time to proportionality n , O ( n ), linear time .
Focusing on the individual timings of the work, you can think about writing '-' instead of "-" and such silly details. Instead, focusing on clarity could focus on making this code more understandable than with a loop. For instance. using the appropriate string constructor:
string s( n, '-' );
Wow!
Finally, the third reason not to sweat in the details is that in general this is a very small part of the code that disproportionately increases the execution time. And the definition of this part (or parts) is not easy to do by simply analyzing the code. Measurements are necessary, and this kind of measurement "where he spends time" is called profiling .
How to find out the answer to a question
Twenty or thirty years ago, you could get a reasonable idea of ​​the effectiveness of individual operations by simply looking at the generated machine code.
For example, you can view the machine code by running the program in the debugger, or you use the approiate option to ask the compiler to generate a list of assembly languages. Note for g ++: the -masm=intel option is convenient for telling the compiler not to generate an ungrokkable AT & T syntax assembly, but instead of Intel syntax. For example, Microsoft assembler uses Intel extended syntax.
Today, a computer processor is more intelligent. It can execute instructions out of order and even before their effect is needed for the "current" point of execution. The compiler can predict that (by incorporating effective knowledge obtained from measurements), but a person has little chance.
The only resource for an ordinary programmer is to measure .
Measurement, measurement, measurement!
And in general, this includes performing a thing that needs to be measured, a million times and divided by a million.
Otherwise, start time and downtime dominate, and the result is garbage.
Of course, if the generated machine code is the same, then the measurement will not tell you anything useful regarding the relative difference. He can then only indicate something about how large the measurement error is. Because you know that there should be zero difference.
Why measurement is the right approach
Suppose that the theoretical considerations in the SO answer indicate that x >= -1 will be slower than x > 0 .
The compiler can surpass any such theoretical consideration by creating horrible code for this x > 0 , possibly because of the contextual possibility of "optimization", which it then (unfortunately!) Recognizes.
A computer processor can also make a mess of prediction.
Thus, in any case, you have to measure.
This means that the theoretical examination did not bring you anything useful: you will still do the same, namely: measurement.
Why is this detailed answer, although apparently useful, IMHO really not
Personally, I would prefer the single word "measure" as the answer.
Due to the fact that it comes down to.
Everything that the reader can not only figure out on his own, but will also have to find out the details anyway; so this is just a wording to try to describe it here, really.
Literature:
[1] Whip, Donald. Structured Programming with the Transition to Reports , ACM Journal Computing Surveys, Volume 6, No. 4, December 1974. p. 268.