For Programmers: Free Programming Magazines  


Home > Archive > Prolog > June 2007 > SWI Prolog: runtime in milliseconds









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author SWI Prolog: runtime in milliseconds
Michael Felderer

2007-06-25, 8:05 am

Hi all,

is there a way to fetch the runtime in SWI Prolog in milliseconds. I
tried to use the statistics predicate with keys cputime, runtime,
system_time and real_time but they all return values in seconds which is
not fine-grained enough for my application (I want to compare an OCL
implementation in Prolog with a Java implementation)

thanks in advance,
Michael Felderer
Jan Wielemaker

2007-06-25, 8:05 am

On 2007-06-25, Michael Felderer <michael.felderer@uibk.ac.at> wrote:
> Hi all,
>
> is there a way to fetch the runtime in SWI Prolog in milliseconds. I
> tried to use the statistics predicate with keys cputime, runtime,
> system_time and real_time but they all return values in seconds which is
> not fine-grained enough for my application (I want to compare an OCL
> implementation in Prolog with a Java implementation)
>
> thanks in advance,
> Michael Felderer


statistics(runtime, X) is Quintus/SICStus/YAP and a few more compatible
and returns a list, whose first member is the user CPU in milliseconds
and the second is the user CPU time since the last call.

statistics(cputime, X) gives the time in seconds as a *float*.

The granularity is system dependent. That is (in my view) the good point
of returning a float. Seconds is the unit of time and IEEE doubles can
both deal with high granularity and long runtimes.

Underlying calls are GetProcessTimes() for Windows and times() for
POSIX systems. The method to get the times() granularity varies
between OSes. It is quite possible there are OSes for which it
reports wrong values. It tries sysconf(_SC_CLK_TCK) and the constant
HZ. See pl-os.c, CpuTime() for details.

What OS are you running?

Cheers --- Jan
Michael Felderer

2007-06-25, 8:05 am

>
> What OS are you running?
>


Actually I am running Windows XP on a 32bit machine.

In principle we can also use (Fedora)Linux also on a 32 bit machine but
we prefer windows xp. Would statistics return milliseconds on Linux?

what procedure do you prefer in my situation because the granularity of
seconds is not enough for our scenario.


thanks,
Michael Felderer
Jan Wielemaker

2007-06-25, 10:06 pm

On 2007-06-25, Michael Felderer <michael.felderer@uibk.ac.at> wrote:
>
> Actually I am running Windows XP on a 32bit machine.
>
> In principle we can also use (Fedora)Linux also on a 32 bit machine but
> we prefer windows xp. Would statistics return milliseconds on Linux?
>
> what procedure do you prefer in my situation because the granularity of
> seconds is not enough for our scenario.


The Windows API returns 100 nanoseconds steps. Don't know the actual
resolution, but it surely is better than seconds. What makes you
think Prolog returns seconds? Do you have a code fragment how to
obtain and process the time information?

--- Jan
Michael Felderer

2007-06-25, 10:06 pm

> The Windows API returns 100 nanoseconds steps. Don't know the actual
> resolution, but it surely is better than seconds. What makes you
> think Prolog returns seconds? Do you have a code fragment how to
> obtain and process the time information?


I try to execute the following sequence. First I get the cpu time, then
i call the Query for which i want to measure the runtime, then I fetch
the cputime again and finally i build the difference of the two cputimes:

statistics(cputime,Time1),
call(Query,m1,Result),
statistics(cputime,Time2),
Time is Time2 - Time1,

I execute 8 queries and get

query_1 0.03125
query_2 0.0
query_3 0.0
query_4 0.0
query_5 0.0
query_6 0.015625
query_7 0.015625
query_8 3.51563

as console output which i cannot really interpret. i first thought this
function returns something like seconds because there is no difference
between pre and after state of the execution.

My queries are of form:

query_1(Ns,Num) :- findall(Id,Ns:me(_#Id,_),IdList),
length(IdList,Num).

,i.e. they operate on the Prolog database (in case of this query with
around 10.000 entries) and compute the number of elements of a specific
type. I cannot understand why queries 2 to 5 return 0 because they are
more complicted than query_1 and why query_6 and query_7 return exactly
the same result.

thanks,
Michael Felderer
Jan Wielemaker

2007-06-25, 10:06 pm

On 2007-06-25, Michael Felderer <michael.felderer@uibk.ac.at> wrote:
>
> I try to execute the following sequence. First I get the cpu time, then
> i call the Query for which i want to measure the runtime, then I fetch
> the cputime again and finally i build the difference of the two cputimes:
>
> statistics(cputime,Time1),
> call(Query,m1,Result),
> statistics(cputime,Time2),
> Time is Time2 - Time1,
>
> I execute 8 queries and get
>
> query_1 0.03125
> query_2 0.0
> query_3 0.0
> query_4 0.0
> query_5 0.0
> query_6 0.015625
> query_7 0.015625
> query_8 3.51563
>
> as console output which i cannot really interpret. i first thought this
> function returns something like seconds because there is no difference
> between pre and after state of the execution.
>
> My queries are of form:
>
> query_1(Ns,Num) :- findall(Id,Ns:me(_#Id,_),IdList),
> length(IdList,Num).
>
> ,i.e. they operate on the Prolog database (in case of this query with
> around 10.000 entries) and compute the number of elements of a specific
> type. I cannot understand why queries 2 to 5 return 0 because they are
> more complicted than query_1 and why query_6 and query_7 return exactly
> the same result.


Looks like 0.015625 is the granularity of the CPU time as reported by
your version of Windows. All the others are a nice multiple of it. I
don't know the exact logic Windows uses to charge a process a unit of
CPU time, but for most OSes this is pretty unreliable. Make sure to
have at least a few seconds of CPU time to get any reliable figure.
Make sure the machine is as quiet as possible. Run the measurement
several times, remove extreme results and average. In most cases this
implies you have to make the problem `big enough', either using bigger
input or run the process in a loop. If you use a loop, run the
problem once before timing to ensure lazy initialization, filling
caches, etc. is all done.

--- Jan
Markus Triska

2007-06-25, 10:06 pm

Michael Felderer <michael.felderer@uibk.ac.at> writes:

> statistics(cputime,Time1),
> call(Query,m1,Result),
> statistics(cputime,Time2),
> Time is Time2 - Time1,


Generalised to average over N evaluations:

statistics(cputime, Time1),
ignore((between(1,N,_), call(Query,m1,Result), fail)),
statistics(cputime, Time2),
Time is (Time2 - Time1)/N,

--
comp.lang.prolog FAQ: http://www.logic.at/prolog/faq/
J. Burse

2007-06-25, 10:06 pm

Michael Felderer schrieb:
> Hi all,
>
> is there a way to fetch the runtime in SWI Prolog in milliseconds. I
> tried to use the statistics predicate with keys cputime, runtime,
> system_time and real_time but they all return values in seconds which is
> not fine-grained enough for my application (I want to compare an OCL
> implementation in Prolog with a Java implementation)
>
> thanks in advance,
> Michael Felderer

Run your test 1000-times in sequence and
measure total time in seconds. Divide
result by 1000, and you get milliseconds.

Advantage of this method: You get also
mean runtime, runtime variantes by GC
or whatever are washed out.

Best Regards
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com