In C++, which is better i>-1 or i>=0 [duplicate]
Asked Answered
P

3

10

This might be a silly question to ask, but this kind of optimization is sometimes boost performance of your application. Here I am asking specifically for C++, because the way C++ compile code is a lot different that c# or Java.

The question is which one performs better, if variable i is int.

  1. i > -1
  2. i >= 0

I am looking for performance in terms of memory block or registers required and CPU cycles required for both conditions.

Thanks in advance.

Portrait answered 22/5, 2015 at 10:57 Comment(8)
The generated assembly code is nearly same. I don't think there is any difference here.Hunnicutt
Check this out.Climb
Micro-Optimization is the root of all evil.Pycnometer
Depends a lot on the instruction set. Some have special instructions for test against zero.Detonation
Check this too: #14521330Ilyssa
that depends if i is an integer or not!Lactoprotein
i is integer, indeed.Portrait
The compiler will use whichever one is faster anyway so it doesn't matter.Ladner
A
16

In assembly language, both are on the same structure:

  1. i > -1

    cmp   [register with i value],-1
    jg    [somewhere]
    
  2. i >= 0

    cmp   [register with i value],0
    jge   [somewhere]
    

According to used jump flags, the instruction jg make two flags comparaisons (ZF = 0 and SF = OF) but jge does only one (SF = OF).

So I'm tempted to say that both use almost same registers and CPU cycles, with maybe a very little quicker comparaison for i >= 0.

Assessor answered 22/5, 2015 at 11:3 Comment(6)
Even 2 flag comparisons instead of one would take the same number of cycles, no?Laclos
@Laclos I don't think so. Take the same sample in programming code: what is the slower between if (something) and if (something && something_else) ?Assessor
Loading the immediate value -1 could be more expensive or require more bytes. Unclear, whether the JIT optimized one form to the other.Clifford
@Clifford No. Take a look: intel.com/content/dam/www/public/us/en/documents/manuals/… Every jcc (jump if condition) are grouped together in the Latency/Throughput table in appendix C.Laclos
@Laclos I'm talking about loading the constant -1, not about the jump. Clearly, all jumps and comparisons have the same cost (a tiny one).Clifford
Both -1 and 0 will take a byte in machine code. The byte is read in full from memory, taking the very same time (same instruction). It would take longer if you compared to someting >255Laclos
Z
2

Well, according to logic > operation may be "cheaper" than >=, but I guess you are compiling with Optimization option enabled, so probably the compiler do whatever he wants to optimize your code, so I would say that doesn't matter, even if one is really faster, probably the compiler change it to the best option

Zaragoza answered 22/5, 2015 at 11:2 Comment(0)
P
0

When writing a for loop it could be beneficial to convert it from for (i = 0; i < 1000; i++) to for (i = 1000; i > 0; i--) because on some architectures the compiler can skip the compare instruction as a flag will be set when i reaches 0. On modern architectures I'm not sure it matters.

Pyridine answered 22/5, 2015 at 11:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.