Omdat ik laatst bezig was met een programma dat veel delingen moest doen..
kwam ik erachter dat het nogal veel snelheid slurpte.. toen hoorde ik van een oude docent dat vroeger snelheids winst was te halen door ipv y:= x/100; y:= x*(1/100) te doen. dus heb ik dat eens getest, en inderdaad, dat scheelt een berg aan snelheid..
ligt aan de cpu natuurlijk, maar meestal kom ik toch wel factor 5 verschil tegen.
heb maar meteen ff een klein projectje gemaakt met voorbeeld.
Code:
Simple Number division:
Calculating
First Pass Result: 2147483,64
Second Pass Result: 2147483,64
Done, Results:
/ 100 Time: 11,9195 seconds
/ 100 Clock: 42666537 ticks
* 0.01 Time: 2,3904 seconds
* 0.01 Clock: 8556545 ticks
Odd Number division:
First Pass Result: 386238,064748
Second Pass Result: 386238,064748
Done, Results:
/ 556 Time: 11,6495 seconds
/ 556 Clock: 41699892 ticks
* (1/556) Time: 2,4255 seconds
* (1/556) Clock: 8682027 ticks
(1/556) = 0,00179856115108 (approximate)
source:
Code:
program DivVsMult;
{$APPTYPE CONSOLE}
uses
SysUtils,
Windows;
var
Start,Stop, Start2, Stop2, Freq:int64;
i : integer;
t : real;
CpuSpeed : integer;
begin
{ TODO -oUser -cConsole Main : Insert code here }
{ Cpu Speed fastes cpu = 1 slower => 10
it's just to determin the number of time to do the loop
Maxint div CpuSpeed is calculated }
if ParamCount = 1 then
CpuSpeed := StrToIntDef(ParamStr(1),1)
else
CpuSpeed := 10;
Writeln('Simple Number division:');
WriteLn('Calculating');
QueryPerformanceFrequency(freq);
QueryPerformanceCounter(Start);
for i:=0 to MaxInt div CpuSpeed do
t := i / 100;
QueryPerformanceCounter(Stop);
WriteLn(Format('First Pass Result: %f',[t]));
{ This is needed because the compiler would optimize,
and would notice the result of the loop isn't used at all,
so therefor the result is useless.. so depending on the compiler, it will
choose what to do with it, this disables that optimization
}
QueryPerformanceCounter(Start2);
for i:=0 to MaxInt div CpuSpeed do
t := i * (1/100);
QueryPerformanceCounter(Stop2);
WriteLn(Format('Second Pass Result: %15.6f',[t]));
{ This is needed because the compiler would optimize,
and would notice the result of the loop isn't used at all,
so therefor the result is useless.. so depending on the compiler, it will
choose what to do with it, this disables that optimization
}
WriteLn('Done, Results:');
Writeln(Format('/ 100 Time: %6.4f seconds'+#13#10+
'/ 100 Clock: %d ticks'+#13#10+
'* 0.01 Time: %6.4f seconds'+#13#10+
'* 0.01 Clock: %d ticks',[(Stop-Start) / freq, (Stop-Start), (Stop2-Start2) / freq, (Stop2-Start2)]));
Writeln;
Writeln('Odd Number division:');
QueryPerformanceCounter(Start);
for i:=0 to high(i) div CpuSpeed do
t := i / 556;
QueryPerformanceCounter(Stop);
WriteLn(Format('First Pass Result: %15.6f',[t]));
{ This is needed because the compiler would optimize,
and would notice the result of the loop isn't used at all,
so therefor the result is useless.. so depending on the compiler, it will
choose what to do with it, this disables that optimization
}
QueryPerformanceCounter(Start2);
for i:=0 to high(i) div CpuSpeed do
t := i * (1/556);
QueryPerformanceCounter(Stop2);
WriteLn(Format('Second Pass Result: %15.6f',[t]));
{ This is needed because the compiler would optimize,
and would notice the result of the loop isn't used at all,
so therefor the result is useless.. so depending on the compiler, it will
choose what to do with it, this disables that optimization
}
WriteLn('Done, Results:');
WriteLn(Format('/ 556 Time: %6.4f seconds'+#13#10+
'/ 556 Clock: %d ticks'+#13#10+
'* (1/556) Time: %6.4f seconds'+#13#10+
'* (1/556) Clock: %d ticks',[(Stop-Start) / freq, (Stop-Start), (Stop2-Start2) / freq, (Stop2-Start2)]));
Writeln(Format(' (1/556) = %15.14f (approximate)',[1/556]));
// readln;
end.
Bookmarks