April 30, 2006 Re: Compiler optimizations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Black | >>> int divTest2(int divisor, int total) >>> { >>> int sum = 0; >>> for(int i = 0; i < total; i++) >>> { >>> int quotient = i * ( 1.0 / divisor ); // !!!!!!!! >>> sum += quotient; >>> } >>> } you say that integer devision is slower then the same division in floating point - or? but what is the problem with my benchmark then? --- sorry in c --- #include <stdio.h> #include <conio.h> #include <time.h> int main() { int result = 0; int div = 10; //(or double) !!! clock_t start, finish; double duration; start = clock(); for(int l=0; l<100000; l++) { for(int i=0; i<10000; i++) { result += (i*div) / div; } } finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; printf("[%i] %2.1f seconds\n",result,duration); getch(); } on 3Ghz: int: ~4.8 seconds double: ~41.7 seconds - nearly ten times slower the problem with your benchmark is that your results are not really usable try to write an "int doIntDivisionWithFloat(int Value, int Div)" and benchmark this one - not do the benchmark on the half of such an function... int IntDivUsingInt(int divisor, int value) { return value/divisor; } int IntDivUsingFloat(int divisor, int value) { return value * (1.0 / divisor); } for(int i = 0; i < longlongtime; i++) { do IntDivUsingInt( 10, i*10 ); or IntDivUsingFloat( 10, i*10 ); } is your IntDivUsingFloat still faster? ciao dennis |
April 30, 2006 Re: Compiler optimizations | ||||
---|---|---|---|---|
| ||||
Posted in reply to dennis luehring | and if still don't understand your "optimizing" try to write an "real" benchmark with value and divisor change every step your tests just show us - wow! multipling is much faster then dividing - and that is something we all know very well (in float and int) :-) ciao dennis |
April 30, 2006 Re: Compiler optimizations | ||||
---|---|---|---|---|
| ||||
Posted in reply to dennis luehring | Yours used a constant divisor which allowed the compiler to optimize it somehow. That's one of the things that I'm trying to figure out. How does the compiler optimize integer division so well when the divisor is constant? Anyway, try this one. #include <stdio.h> #include <conio.h> #include <time.h> //typedef int divtype; typedef double divtype; // this one is faster int Division(divtype div) { int result = 0; for(int i=0; i<10000; i++) { result += i / div; } return result; } int main() { int result = 0; int div = 10; //(or double) !!! clock_t start, finish; double duration; start = clock(); for(divtype div=1; div<10000; div++) { result = Division(div); } finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; printf("[%i] %2.1f seconds\n",result,duration); } |
April 30, 2006 Re: Compiler optimizations | ||||
---|---|---|---|---|
| ||||
Posted in reply to dennis luehring | > your tests just show us - wow! multipling is much faster then dividing -
> and that is something we all know very well (in float and int) :-)
No, actually it shows us that integer division can be slower than floating point division. This is because it uses floating point division under the hood and requires that the divisor be converted from int to float.
-Craig
|
April 30, 2006 Re: Compiler optimizations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Black | In article <e32q1p$22dv$1@digitaldaemon.com>, Craig Black says... > >These two functions show the difference that I'm talking about. When benchmarking, pass in the same values for the divisor (pick a value between 2 and 30), and pass in a large enough value for total so that it will take some time for the CPU to finish. Suprisingly, the second one is faster. > >-Craig > >int divTest1(int divisor, int total) >{ > int sum = 0; > for(int i = 0; i < total; i++) > { > int quotient = i / divisor; > sum += quotient; > } >} > >int divTest2(int divisor, int total) >{ > int sum = 0; > double idiv = 1.0 / divisor; > for(int i = 0; i < total; i++) > { > int quotient = i * idiv; > sum += quotient; > } >} > The second one is faster because you cheat. You aren't testing integer division but rather floating point multiplication. The first one has to divide every time through the loop, the second one only has to multiply. Multiplication is much faster than division (even floating point multiplication vs integer division), so your results are not surprising and are not really useful. |
April 30, 2006 Re: Compiler optimizations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Phillips | > The second one is faster because you cheat. Nope. Try this one. On my computer the floating point division is twice as fast. I believe this is due to the overhead on converting the divisor from int to double before performing the division. #include <stdio.h> #include <conio.h> #include <time.h> //typedef int divtype; typedef double divtype; // this one is faster int main() { int result = 0; clock_t start, finish; double duration; start = clock(); for(divtype div=1; div<10000; div++) { for(int i=0; i<10000; i++) { result += i / div; } } finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; printf("[%i] %2.1f seconds\n",result,duration); } |
April 30, 2006 Re: Compiler optimizations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Black | Craig Black schrieb:
> Nope. Try this one. On my computer the floating point division is twice as
sorry but on my system int division is two times faster than floating point
and by the way - the usage of the "double" in the first loop don't realy help to focus the bechmarking (incrementing int is much faster them incerenting floating points)
ciao dennis
|
April 30, 2006 Re: Compiler optimizations | ||||
---|---|---|---|---|
| ||||
Posted in reply to dennis luehring | This is probably because you have SSE optimizations disabled. This one works even without SSE. This shows that integer division is slower. Also, if you change the division to multiplication, you will notice that integer multiplication is faster, which is what you would expect. #include <stdio.h> #include <conio.h> #include <time.h> //typedef int divtype; typedef double divtype; // This one is faster int main() { divtype result = 0; clock_t start, finish; double duration; start = clock(); divtype max = 100000000; for(divtype div=1; div<max; div++) { divtype i = max - div; result += i / div; } finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; printf("[%f] %2.1f seconds\n",double(result),duration); } |
May 01, 2006 Re: Compiler optimizations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Black | Craig Black schrieb:
> This is probably because you have SSE optimizations disabled.
> This one works even without SSE. This shows that integer division is
> slower. Also, if you change the division to multiplication, you will notice
> that integer multiplication is faster, which is what you would expect.
your right the double version is faster - i don't think that it can be used with integer division - because the to-float and back-to-int conversion costs too much...
ciao dennis
|
May 01, 2006 Re: Compiler optimizations | ||||
---|---|---|---|---|
| ||||
Posted in reply to dennis luehring | "dennis luehring" <dl.soluz@gmx.net> wrote in message news:e34ak8$17sg$1@digitaldaemon.com... > Craig Black schrieb: >> This is probably because you have SSE optimizations disabled. >> This one works even without SSE. This shows that integer division is >> slower. Also, if you change the division to multiplication, you will >> notice >> that integer multiplication is faster, which is what you would expect. > > your right the double version is faster - i don't think that it can be used with integer division - because the to-float and back-to-int conversion costs too much... > > ciao dennis Not if SSE is enabled. I have actually experienced significant performance increases by doing so. -Craig |
Copyright © 1999-2021 by the D Language Foundation