- Code: Select all
/*
Optimize code by shifting
*/
unsigned __int16
__cdecl printf(const char*, ...),
__cdecl getch(void);
void main(void) {
Instead of dividing 'a' with 16 (2^4) we shift
its bits four positions to the right.
By using this we can skip the consuming;
mov ecx, 16 <-- 1c
mov eax,dword ptr [a] <-- 1c
cdq <-- 3c
idiv ecx <-- 46c
mov dword ptr [a], eax <-- 1c
And replace it with;
sar dword ptr [a], 4 <-- 3c
Dividing 'a' with 16 will need 52c (cycles),
and shifting bits will only need 3c (cycles)!
That is an impressive optimization
The following code will show you how the
optimizing is done using C code:
- Code: Select all
unsigned __int16 a = 0x64; // a = 100
a >>= 4; // a = a/16
printf("%d\x0A",a); // send result to stout
getch(); // wait for kbhit
}