Performance was not as expected
Latest Revision
Hi everyone
I wrote a function to get the exponent of a number power of two. Here is the code:
int exp = 0;
while((n = n / 2) > 0) {
exp++;
}
return exp;
Then came to mind, "Well, let's replace that division operator to the shift operator, to improve performance".
The result was not the expected.
Using the division operator, the code takes about 1 second; using the shift operator, the code takes about 1.3 seconds, on same conditions.
In debug mode, I checked the disassmebly code gave me by Visual Studio (Note: I don't understand nothing about assembly language). I noticed that the assembly code was the same, except when I am using the division operator, there are two more instruction.
So, can anyone explain to me the reason of this time execution difference? I expected, in worst case, the same execution time.
Thanks in advance
Filipe Marques
UPDATE 2: Add the full asm code for release mode.
For the divion operator:
PUBLIC ?get_exponent@@YAHH@Z
_TEXT SEGMENT
?get_exponent@@YAHH@Z PROC
cdq
sub eax, edx
sar eax, 1
xor ecx, ecx
test eax, eax
jle SHORT $LN7@get_expone
npad 5
$LL2@get_expone:
cdq
sub eax, edx
sar eax, 1
inc ecx
test eax, eax
jg SHORT $LL2@get_expone
$LN7@get_expone:
mov eax, ecx
ret 0
?get_exponent@@YAHH@Z ENDP
For shift operator:
PUBLIC ?get_exponent@@YAHH@Z
_TEXT SEGMENT
?get_exponent@@YAHH@Z PROC
sar ecx, 1
xor eax, eax
test ecx, ecx
jle SHORT $LN1@get_expone
$LL2@get_expone:
sar ecx, 1
inc eax
test ecx, ecx
jg SHORT $LL2@get_expone
$LN1@get_expone:
ret 0
?get_exponent@@YAHH@Z ENDP
Posted 26-Jan-13 6:59am
by
Filipe Marques.