Search code examples
timeprologswi-prolog

Is it possible to get elapsed time in microseconds in Swi-Prolog?


Simple question.

I need to calculate how long it takes to execute predicate X. So I wrote this predicate:

chronometrise(X) :-
    write('Executing: '), write(X), nl, nl,
    statistics(walltime, _), call(X), statistics(walltime, [_,E]),
    nl, write('Time: '), write(E), write(' ms.'), nl.

This allows me to get time in milliseconds. But I need microseconds.

Is it possible to calculate this in Swi-Prolog? Or milliseconds is best accuracy?

EDIT: Well, I found a better way: execute predicate 1000 times and calculate elapsed time. This will give us average execution time in milliseconds multiplyed by 1000, which is exactly average elapsed time in microseconds.


Solution

  • Instead of using statistics(walltime, _), use get_time(Time). This returns a wall time stamp as a float. The resolution of the reported time depends on the OS. The walltime statistics originates from Quintus compatibility.