I'm trying to benchmark a (SWI-)Prolog program that may take several seconds to finish. I'd like to save CPU-time and memory statistics through time to then be able to show a sort of evolution plot. Something similar to the system monitor, but just with my program.
For that I tried using alarm/4
:
stat_start(Id) :-
alarm(0.25,stat_point,Id,[remove(false),install(true)]). % 250 milliseconds
stat_stop(Id) :-
remove_alarm(Id).
stat_point :-
stat_cpu, % calls statistics/2 and appends values to a CSV file
stat_mem. % calls statistics/2 and appends values to a CSV file
I simply can't get proper timing between each stat_point
. The timing varies from milliseconds to seconds and I can't do anything about it. And changing alarm/4
's time does't make any difference. I also tried a simple query like:
?- alarm(1, write('hello\n'), Id, [remove(false),install(true)]),
repeat,
fail.
And it doesn't work either. I'm just getting a single "hello". I thought that a solution might be to insert calls to stat_point
within my code, but it doesn't look rather elegant, does it? Besides, the points wouldn't be equally spaced.
Is there a proper way to monitor a Prolog program in a timed-fashion? Would profile/1
provide this kind of information somehow?