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

**URL:** <https://swi-prolog.discourse.group/t/how-can-i-calculate-execution-time-of-program-in-milliseconds-in-prolog/4270>\
**Category:** Predicate\
**Created:** [August 9, 2021, 7:26pm UTC](https://swi-prolog.discourse.group/t/how-can-i-calculate-execution-time-of-program-in-milliseconds-in-prolog/4270 "2021-08-09T19:26:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rohamfarahani](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/rohamfarahani/32/3663_2.png) [@rohamfarahani](https://swi-prolog.discourse.group/u/rohamfarahani)\
**Post date:** [August 9, 2021, 7:26pm UTC](https://swi-prolog.discourse.group/t/how-can-i-calculate-execution-time-of-program-in-milliseconds-in-prolog/4270/1 "2021-08-09T19:26:37Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [August 9, 2021, 7:53pm UTC](https://swi-prolog.discourse.group/t/how-can-i-calculate-execution-time-of-program-in-milliseconds-in-prolog/4270/2 "2021-08-09T19:53:41Z")

</div>

Are you looking for something like this

```prolog
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](https://swi-prolog.discourse.group/t/database-micro-benchmark-discussion/3208/6) by @swi

* * *

**EDIT**

Also consider if trim\_stacks/0 and such is necessary.

---

<div class="post-metadata">

**Author:** ![dgelessus](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/dgelessus/32/3106_2.png) [@dgelessus](https://swi-prolog.discourse.group/u/dgelessus)\
**Post date:** [August 9, 2021, 8:06pm UTC](https://swi-prolog.discourse.group/t/how-can-i-calculate-execution-time-of-program-in-milliseconds-in-prolog/4270/3 "2021-08-09T20:06:39Z")

</div>

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

```prolog
?- 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.

> [@rohamfarahani](#):
>
> I found the following source code but I don’t know , what can set the input parameters for example TimeSinceStart parameter.

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:

```prolog
?- 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.

---

<div class="post-metadata">

**Author:** ![rohamfarahani](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/rohamfarahani/32/3663_2.png) [@rohamfarahani](https://swi-prolog.discourse.group/u/rohamfarahani)\
**Post date:** [August 9, 2021, 8:13pm UTC](https://swi-prolog.discourse.group/t/how-can-i-calculate-execution-time-of-program-in-milliseconds-in-prolog/4270/4 "2021-08-09T20:13:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rohamfarahani](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/rohamfarahani/32/3663_2.png) [@rohamfarahani](https://swi-prolog.discourse.group/u/rohamfarahani)\
**Post date:** [August 9, 2021, 8:23pm UTC](https://swi-prolog.discourse.group/t/how-can-i-calculate-execution-time-of-program-in-milliseconds-in-prolog/4270/5 "2021-08-09T20:23:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [August 9, 2021, 8:42pm UTC](https://swi-prolog.discourse.group/t/how-can-i-calculate-execution-time-of-program-in-milliseconds-in-prolog/4270/6 "2021-08-09T20:42:22Z")

</div>

If you want CPU time:

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

```

---

<div class="post-metadata">

**Author:** ![rohamfarahani](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/rohamfarahani/32/3663_2.png) [@rohamfarahani](https://swi-prolog.discourse.group/u/rohamfarahani)\
**Post date:** [August 10, 2021, 6:40am UTC](https://swi-prolog.discourse.group/t/how-can-i-calculate-execution-time-of-program-in-milliseconds-in-prolog/4270/7 "2021-08-10T06:40:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [August 10, 2021, 1:26pm UTC](https://swi-prolog.discourse.group/t/how-can-i-calculate-execution-time-of-program-in-milliseconds-in-prolog/4270/8 "2021-08-10T13:26:12Z")

</div>

> [@rohamfarahani](#):
>
> 4500 rules in a knowledge base

See: [library(prolog\_jiti)](https://www.swi-prolog.org/pldoc/man?section=prologjiti): Just In Time Indexing (JITI) utilities

HTH

---

<div class="post-metadata">

**Author:** ![rohamfarahani](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/rohamfarahani/32/3663_2.png) [@rohamfarahani](https://swi-prolog.discourse.group/u/rohamfarahani)\
**Post date:** [August 10, 2021, 1:55pm UTC](https://swi-prolog.discourse.group/t/how-can-i-calculate-execution-time-of-program-in-milliseconds-in-prolog/4270/9 "2021-08-10T13:55:04Z")

</div>

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
