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.
i > -1
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.
Answer
In assembly language, both are on the same structure:
i > -1
cmp [register with i value],-1
jg [somewhere]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
.
No comments:
Post a Comment