c++ - How to let GCC compiler turn variable-division into mul(if faster) -
int a, b; scanf("%d %d", &a, &b); printf("%d\n", (unsigned int)a/(unsigned char)b);
when compiling, got ...
::00401c1e:: c70424 24304000 mov dword ptr [esp],403024 %d %d ::00401c25:: e8 36ffffff call 00401b60 scanf ::00401c2a:: 0fb64c24 1c movzx ecx,byte ptr [esp+1c] ::00401c2f:: 8b4424 18 mov eax,[esp+18] ::00401c33:: 31d2 xor edx,edx ::00401c35:: f7f1 div ecx ::00401c37:: 894424 04 mov [esp+4],eax ::00401c3b:: c70424 2a304000 mov dword ptr [esp],40302a %d\x0a ::00401c42:: e8 21ffffff call 00401b68 printf
will faster if div turn mul , use array store mulvalue? if so, how let compiler optimization?
int main() { uint a, s=0, i, t; scanf("%d", &a); diviuint aa = a; t = clock(); (i=0; i<1000000000; i++) s += i/a; printf("result:%10u\n", s); printf("time:%12u\n", clock()-t); return 0; }
where diviuint(a) make memory of 1/a , use multiple instead using s+=i/aa makes speed 2 times of s+=i/a
replacing div mul may make sense (but doesn't have in cases) when 1 of values known @ compile time. when both user inputs, don't know what's range, usual tricks not work.
basically need handle both a
, b
between int_max
, int_min
. there's no space left scaling them up/down. if wanted extend them larger types, take longer time invert b
, check result consistent.
Comments
Post a Comment