# Arithmetic function 'cputime' resolution

**URL:** <https://swi-prolog.discourse.group/t/arithmetic-function-cputime-resolution/5560>\
**Category:** Predicate\
**Created:** [June 28, 2022, 8:10pm UTC](https://swi-prolog.discourse.group/t/arithmetic-function-cputime-resolution/5560 "2022-06-28T20:10:38Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![ridgeworks](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ridgeworks/32/886_2.png) [@ridgeworks](https://swi-prolog.discourse.group/u/ridgeworks)\
**Post date:** [July 4, 2022, 1:46am UTC](https://swi-prolog.discourse.group/t/arithmetic-function-cputime-resolution/5560/14 "2022-07-04T01:46:27Z")

</div>

> [@jan](#):
>
> No clue why you need this.

Motivated by this thread: [Different results when using library(simplex) or R-package lpSolve / lpSolveAPI - #6 by ridgeworks](https://swi-prolog.discourse.group/t/different-results-when-using-library-simplex-or-r-package-lpsolve-lpsolveapi/5545/6)  
I’ve been looking at how to use `library(simplex)` in a CLP framework, the main issue being there’s an API mismatch. One of the main issues is that variables within a CLP system are represented by attributed variables while variables in `simplex` constraints are represented by atoms or compound terms, w.g., `x` or `x(0)`. In building a CLP friendly wrapper for `library(simplex)`, I wanted a light weight mechanism to associate a name with a CLP variable for relatively short period of time.

The first prototype used:

```prolog
simplex_var_(V,var(Vstr)) :- term_string(V,Vstr).

```

This looks up a variable name usable by `simplex` given a constrained variable `V`. `Vstr` is the “address” of the variable `V` converted to a string so it will be unique for any variable of interest. But it’s potentially not safe due to variable relocation during garbage collection. And it’s unlikely to be the most performant solution based on a simple benchmark.

A better approach is to use an attribute to store a unique ID of some kind. Using the cputime (a monotonically increasing value) was the first stab but appears to have platform dependancies and will probably start to fail at microsecond resolution (two successive queries may yield the same value). Using the inference counter neatly solves this problem. So the code becomes:

```prolog
simplex_var_(V,SV) :- 
	(get_attr(V,clpBNR_toolkit,SV)
	 -> true
	 ; statistics(inferences,Vname), SV = var(Vname), put_attr(V,clpBNR_toolkit,SV)
	).
		 
attr_unify_hook(var(_),_). % unification always does nothing and succeeds

```

This works quite nicely and is 10 times faster than a `term_string` based implementation.

Using the new clpBNR wrapper module for Example 2 from [`library(simplex)`](https://www.swi-prolog.org/pldoc/man?section=simplex):

```prolog
?- X1::real(0,1), X2::real(0,2), lin_maximize(7*X1+4*X2,{6*X1+4*X2=<8},Value).
X1 = 1,
X2 = 1r2,
Value = 9.

?- X1::integer(0,1), X2::integer(0,2), lin_maximize(7*X1+4*X2,{6*X1+4*X2=<8},Value).
X1 = 0,
X2 = 2,
Value = 8.

```

The new variable name attribute is removed before the completion of `lin_minimize/3` so there’s no lingering effects.

---

_[View the full topic](https://swi-prolog.discourse.group/t/arithmetic-function-cputime-resolution/5560)._
