How can I measure the difference in code speed between a loop and a recursive method?

Let's say we need to create a calculator and the first function is Fatorial. We can write it as a recursive function or use a loop to get the result. We all know that recursion is slower due to its exponential nature. But how can you prove it with code, not by counting lines?

I tried to calculate the number of milliseconds spent, but with my i7 it is always zero between the start time and the end of the code.

How can I measure the difference in code speed between a loop and a recursive method?

type
  TJanela = class(TForm)
    Instrucao: TLabel;
    Entrada: TEdit;
    Botao: TButton;
    procedure Calcular(Sender: TObject);
  end;

var
  Janela: TJanela;
  Val, Fat, Start, TimeRecursive, TimeLoop: Int64;

function FR(N: Int64): Int64; // Fatorial Recursivo
function FL(N: Int64): Int64; // Fatorial em Loop

implementation

{$R *.dfm}

procedure TJanela.Calcular(Sender: TObject);
begin
  Val := StrToInt(Entrada.Text);
  Start := StrToInt(FormatDateTime('nnsszzz',Now));
  Fat := FR(Valor);
  TimeRecursive := StrToInt(FormatDateTime('nnsszzz',Now)) - Start;
  Start := StrToInt(FormatDateTime('nnsszzz',Now));
  Fat := FL(Valor);
  TimeLoop := StrToInt(FormatDateTime('nnsszzz',Now)) - Start;
  if Val > 25 then
    ShowMessage('Delphi can't calculate above [ 25! ]')
  else
    ShowMessage(' [ ' +
                IntToStr(Val) + '! ] is equal to [ ' +
                FormatFloat('###,###,###,###,###,###',Fat) + ' ]'#13#13+
                'Recursive: [ ' + IntToStr(TimeRecursive) + ' ] ms;'#13+
                'Loop: [ ' + IntToStr(TimeLoop) + ' ] ms;');
end;

function FR(N: Int64): Int64;
begin
  if N <= 1 then
    Result := 1
  else
    Result := N * FR(N - 1);
end;

function FL(N: Int64): Int64;
var
  I: Integer;
begin
  for I := 2 to N - 1 do
    N := N * I;
  if N = 0 then
    Result := 1
  else
    Result := N;
end;

      

Now that David came with the answer, I asked a question about Mathematics so that they could help me come up with two equations to determine the closest moment when a given factorial will spend on the computer in both methods.

+3


source to share


3 answers


You are using a fairly low low resolution timer and the factorial function evaluation alone is too fast to even register.

You can use a timer with a higher resolution, but the easiest way is time, which takes longer. Instead of syncing one factorial call, the time is in a thousand or a million.



If you are really interested in implementing a fast factorial function for integer inputs, you should use a lookup table.

For what it's worth, I think the TStopWatch in the Diagnostics module is more sync-friendly than date / time functions.

+8


source


Use a profiler . Recent versions of Delphi include the limited functionality versions of AQTime , but search profiler Delphi

here comes up Profile and Memory Analysis Tools for Delphi here on StackOverflow.



Profilers allow you to evaluate your code in several ways, including the exact amount of time it takes to execute various parts. You can use the results to determine which one is taking more (or less) time.

+5


source


if it's just for testing, you could put TimeGetTime () instead of GetTime () before and after the loop. then just store the value in a list to see how long it takes.

if it is too slow try QueryPerformanceCounter / QueryPerformanceFrequency

0


source







All Articles