How can I calculate execution time of program in milliseconds in prolog?

I need to calculate the execution time of pro log program, because I searching in 4500 rules in a knowledge base. I found the following source code but I don’t know , what can set the input parameters for example TimeSinceStart parameter.

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

Are you looking for something like this

benchmark_goal(BenchMark,Goal) :-
   get_time(OldWall),
   statistics(cputime, OldCpu),
   call(Goal),
   get_time(NewWall),
   statistics(cputime, NewCpu),
   UsedWall is NewWall - OldWall,
   UsedCpu  is NewCpu  - OldCpu,
   assertz( db_bench(BenchMark, stat{  cpu_time: UsedCpu,
                                       wall_time: UsedWall })),
   print_message(information, bench(UsedCpu, UsedWall)).

which was copied from this reply by @swi


EDIT

Also consider if trim_stacks/0 and such is necessary.

If you just want to see the execution time for debugging, you can use SWI-Prolog’s built-in time/1 predicate:

?- time(some_slow_code).
% 7,940,026 inferences, 1.144 CPU in 1.152 seconds (99% CPU, 6939812 Lips)
true.

(The “1.152 seconds” is the walltime value that you’re looking for.)

But time/1 only prints the time statistics and doesn’t return them. If you want to store or accumulate the times, you need to use statistics/2 directly.

To clarify, statistics/2 doesn’t take any inputs - both entries in the list are output values. TimeSinceStart is the walltime since the Prolog process was started, and TimeSinceLastCall is the time since the last statistics(walltime, ...) call (both in milliseconds). It’s easiest to understand if you try it out interactively:

?- statistics(walltime, Stats).
Stats = [1323, 1323].

?- statistics(walltime, Stats).
Stats = [3010, 1687].

In this example, the first statistics/2 call was 1.3 seconds after Prolog was started, and the second one was 3.0 seconds after Prolog was started and 1.7 seconds after the first call.

Thank you very much for consideration ,my big problem is that do not know what can set the parameters that are with purple color.

That’s how I understood that must use statistics/2 predicate before the first statement : Statistics(wall time, stats) and last statement of pro log program Statistics(wall time, stats) and then subtract two values of stats variable.

If you want CPU time:

statistics(cputime, T0),
do_something,
statistics(cputime, T1),
T is T1 - T0,
format('CPU time: ~w~n', [T]).

Dear Peter,
Many Thanks for your consideration and beneficence, If the CPUTime is total execution time of my program, your SWI-Prolog code is proper for me.

See: library(prolog_jiti): Just In Time Indexing (JITI) utilities

HTH

Dear Eric,
Thank you for consideration and beneficence,
It is a good assist to optimize the program execution time.
B.Regards

   EricGT   

August 10

rohamfarahani:
4500 rules in a knowledge base

See: library(prolog_jiti): Just In Time Indexing (JITI) utilities

HTH