Task

Write a program which uses a timer (with the least granularity available
on your system) to time how long a function takes to execute.

Whenever possible, use methods which measure only the processing time used
by the current process; instead of the difference in system time
between start and finish, which could include time used by
other processes on the computer.

This task is intended as a subtask for Measure relative performance of sorting algorithms implementations.




import cp.
 
go =>
  println("time/1 for 201 queens:"),
  time2(once(queens(201,_Q))),
  nl,
 
  % time1b/1 is a used defined function (using statistics/2)
  Time = time1b($once(queens(28,Q2))),
  println(Q2),
  printf("28-queens took %dms\n", Time),
  nl.
 
% N-queens problem.
% N: number of queens to place
% Q: the solution
queens(N, Q) =>
    Q=new_list(N),
    Q :: 1..N,
    all_different(Q),
    all_different([$Q[I]-I : I in 1..N]),
    all_different([$Q[I]+I : I in 1..N]),
    solve([ffd,split],Q).
 
% time1b/1 is a function that returns the time (ms)
time1b(Goal) = T =>
  statistics(runtime, _),
  call(Goal),
  statistics(runtime, [_,T]).