# Recursion and weird benchmarking

**URL:** <https://swi-prolog.discourse.group/t/recursion-and-weird-benchmarking/4325>\
**Category:** Discussion\
**Created:** [August 21, 2021, 11:32am UTC](https://swi-prolog.discourse.group/t/recursion-and-weird-benchmarking/4325 "2021-08-21T11:32:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wisermans](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/wisermans/32/3113_2.png) [@Wisermans](https://swi-prolog.discourse.group/u/Wisermans)\
**Post date:** [August 21, 2021, 11:32am UTC](https://swi-prolog.discourse.group/t/recursion-and-weird-benchmarking/4325/1 "2021-08-21T11:32:39Z")

</div>

As it seems that benchmarking is a theme than many are interested in, i found that [link interesting to look at](https://stackoverflow.com/questions/36685680/prolog-recursion-in-lists-last-but-one-element) starting from the [P-99 Ninety-NineProblems](https://www.ic.unicamp.br/~meidanis/courses/mc336/problemas-prolog/) definition to the different ways to write it and the impact on performance.

```
% P02 (*): Find the last but one element of a list

% last_but_one(X,L) :- X is the last but one element of the list L
% (element,list) (?,?)

last_but_one(X,[X,_]).
last_but_one(X,[_,Y|Ys]) :- last_but_one(X,[Y|Ys]).

```

It also gives a simple benchmarking in between SWI-Prolog and SICStus as well as “classical” Prolog vs DCG, based on different coding solutions. Some benchmarking figures look weird, especially on the SWI dcg vs dcgx example benchmarking. Any explanation for that 2.15x that grows to 7.89x ?

```prolog
          SICStus SWI
          4.3.2 7.3.20-1
    --------------+----------+--------
    f2 0.090s | 1.449s | 16.10×
    dcg 3.670s | 7.896s | 2.15×
    dcgx 1.000s | 7.885s | 7.89×

```

(f2 is the quickest code provided in their examples)

[See link here for details](https://stackoverflow.com/questions/36685680/prolog-recursion-in-lists-last-but-one-element)

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [August 22, 2021, 6:35am UTC](https://swi-prolog.discourse.group/t/recursion-and-weird-benchmarking/4325/2 "2021-08-22T06:35:02Z")

</div>

> [@Wisermans](#):
>
> `16.10×`

Tried on current versions, resulting in about 6 times (both systems got significantly faster). Also note that the way SWI-Prolog is build (tools, options) makes an up to 50% difference. SICStus has binary releases and I’d suppose they use the best tools. For SWI-Prolog use GCC 9 or later and PGO guide optimization for the build. Some more tweaking can probably enhance this further without any changes to the sources.
