Last Updated: February 25, 2016
·
8.45K
· frapontillo

How to measure execution time in SWI-Prolog

If you plan on doing some heavy computation in Prolog you probably want to log how much time gets spent on a predicate.
To do so, you can rely on the statistics predicate in conjunction with the walltime atom parameter.

?- statistics(walltime, [TimeSinceStart | [TimeSinceLastCall]]),
   some_heavy_operation,
   statistics(walltime, [NewTimeSinceStart | [ExecutionTime]]),
   write('Execution took '), write(ExecutionTime), write(' ms.'), nl.

statistics(walltime, Result) sets Result as a list, with the head being the total time since the Prolog instance was started, and the tail being a single-element list representing the time since the last statistics(walltime, _) call was made.

Reference: statistics/2