Task

Write a program which uses a timer (with the least granularity available
on your system) to time how long a function takes to execute.

Whenever possible, use methods which measure only the processing time used
by the current process; instead of the difference in system time
between start and finish, which could include time used by
other processes on the computer.

This task is intended as a subtask for Measure relative performance of sorting algorithms implementations.




$ include "seed7_05.s7i";
  include "time.s7i";
  include "duration.s7i";
 
const func integer: identity (in integer: x) is
  return x;
 
const func integer: sum (in integer: num) is func
  result
    var integer: result is 0;
  local
    var integer: number is 0;
  begin
    result := num;
    for number range 1 to 1000000 do
      result +:= number;
    end for;
  end func;
 
const func duration: timeIt (ref func integer: aFunction) is func
  result
    var duration: result is duration.value;
  local
    var time: before is time.value;
  begin
    before := time(NOW);
    ignore(aFunction);
    result := time(NOW) - before;
  end func;
 
const proc: main is func
  begin
    writeln("Identity(4) takes " <& timeIt(identity(4)));
    writeln("Sum(4)      takes " <& timeIt(sum(4)));
  end func;