Doing a comparison in C++ with an int is x >= 0 more efficient than x > -1?
Doing a comparison in C++ with an int is x >= 0 more efficient
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
short answer: no.
longer answer to provide some educational insight: it depends entirely on your compiler, allthough i bet that every sane compiler creates identical code for the 2 expressions.
example code:
and then compile and compare the resulting assembler code:
yields this:
_Z8func_ge0i: .LFB0: .cfi_startproc .cfi_personality 0x0,__gxx_personality_v0 movl 4(%esp), %eax notl %eax shrl $31, %eax ret .cfi_endprocvs.
_Z9func_gtm1i: .LFB1: .cfi_startproc .cfi_personality 0x0,__gxx_personality_v0 movl 4(%esp), %eax notl %eax shrl $31, %eax ret .cfi_endproc(compiler: g++-4.4)
conclusion: don’t try to outsmart the compiler, concentrate on algorithms and data structures, benchmark and profile real bottlenecks, if in doubt: check the output of the compiler.