# 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:** 1\
**Showing post:** 2

<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.

---

_[View the full topic](https://swi-prolog.discourse.group/t/how-can-i-calculate-execution-time-of-program-in-milliseconds-in-prolog/4270)._
