Performance was not as expected
Revision 2
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:
Here are the assembly code produced for the two methods:
while((n = n / 2) > 0) {
00CE1BD5 mov eax,dword ptr [n]
00CE1BD8 cdq
00CE1BD9 sub eax,edx
00CE1BDB sar eax,1
00CE1BDD mov dword ptr [n],eax
00CE1BE0 cmp dword ptr [n],0
00CE1BE4 jle get_exponent+41h (0CE1BF1h)
while((n = n >> 1) > 0) {
00B31BD5 mov eax,dword ptr [n]
00B31BD8 sar eax,1
00B31BDA mov dword ptr [n],eax
00B31BDD cmp dword ptr [n],0
00B31BE1 jle get_exponent+3Eh (0B31BEEh)
Posted 26-Jan-13 6:59am
by
Filipe Marques.