Logtalk 2.21.3 files.

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1164 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
pmoura
2004-10-25 11:13:58 +00:00
parent 8e92ce869f
commit c3e3a72583
155 changed files with 569 additions and 219 deletions

View File

@@ -0,0 +1,36 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.21.3
Copyright (c) 1998-2004 Paulo Moura. All Rights Reserved.
=================================================================
To load all entities in this example compile and load the loader file:
| ?- logtalk_load(loader).
If your Prolog compiler does not support a module system, then edit the
loader.lgt file and comment out the directive that loads the module code.
In case your Prolog compiler supports a module system, you may need to
edit the code on the module.pl file and make any necessary compatibility
changes.
This folder provides simple benchmarks for comparing Logtalk message
sending performance with direct calls to Prolog predicates and with
calls to module predicates. These benchmarks may also be used for
comparing Logtalk message sending performance across Prolog compilers.
This example is made of four source files:
benchmark.pl
contains the benchmark predicates
plain.pl
contains a definition for a list length predicate
module.pl
contains the same definition of a list length predicate
encapsulated in a module
object.lgt
contains the same definition of a list length predicate
encapsulated in an object

View File

@@ -0,0 +1,64 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.21.3
Copyright (c) 1998-2004 Paulo Moura. All Rights Reserved.
=================================================================
% start by loading the example:
| ?- logtalk_load(loader).
...
% call the built-in control construct true/0 to measure the overhead of the
% benchmark/1 predicate itself:
| ?- benchmark(true).
Number of repetitions: 1000000
Average time per call: 1.3e-07 seconds
Number of calls per second: 7692307.6923077
yes
% call the predicate my_length/0 defined in the Prolog database:
| ?- benchmark(my_length([1, 2, 3, 4, 5, 6, 7, 8, 9, 0], _)).
Number of repetitions: 1000000
Average time per call: 1.4e-07 seconds
Number of calls per second: 7142857.14285716
yes
% call the predicate module:mod_length/2 from top-level:
| ?- benchmark(module:mod_length([1, 2, 3, 4, 5, 6, 7, 8, 9, 0], _)).
Number of repetitions: 1000000
Average time per call: 1.4e-07 seconds
Number of calls per second: 7142857.14285716
yes
% call the predicate list::length/2 from top-level:
| ?- benchmark(object::length([1, 2, 3, 4, 5, 6, 7, 8, 9, 0], _)).
Number of repetitions: 1000000
Average time per call: 1.4e-07 seconds
Number of calls per second: 7142857.14285716
yes
% compiled call of the predicate list::length/2 (simulates message sending
% from a compiled object to another object; thus no top-level overhead):
| ?- benchmark('$lgt_send_to_object_nv'(object, length([1, 2, 3, 4, 5, 6, 7, 8, 9, 0], _), user)).
Number of repetitions: 1000000
Average time per call: 1.4e-07 seconds
Number of calls per second: 7142857.14285716
yes

View File

@@ -0,0 +1,38 @@
% benchmark a goal using a default number of repetitions and printing some
% useful statistics
benchmark(Goal) :-
N = 10000000,
write('Number of repetitions: '), write(N), nl,
'$lgt_cpu_time'(Seconds1), % defined in the config files
benchmark(N, Goal),
'$lgt_cpu_time'(Seconds2),
Average is (Seconds2 - Seconds1)/N,
write('Average time per call: '), write(Average), write(' seconds'), nl,
Speed is 1.0/Average,
write('Number of calls per second: '), write(Speed), nl.
% repeat a goal N times using a failure-driven loop to avoid the interference
% of Prolog compiler memory management mechanism (such as garbage collection)
% on the results
benchmark(N, Goal) :-
repeat(N), % another option would be to use a between/3 built-in predicate
call(Goal),
fail.
benchmark(_, _).
% some Prolog compilers define the predicate repeat/1 as a built-in predicate;
% if that's the case of the Prolog compiler you are using, then comment out
% the definition that follows
repeat(0) :-
!.
repeat(N) :-
N2 is N - 1,
repeat(N2).

View File

@@ -0,0 +1,12 @@
% if your Prolog compiler does not support the ensure_loaded/1 directive
% then simply consult the files
:- ensure_loaded(benchmark).
:- ensure_loaded(plain).
% comment the next line if your Prolog compiler does not support modules
:- ensure_loaded(module).
:- initialization(logtalk_load(object)).

View File

@@ -0,0 +1,28 @@
% you may need to update the module directive that follows for
% compatibility with your Prolog compiler module system
:- module(module, [mod_length/2]).
mod_length(List, Length) :-
integer(Length) ->
Length >= 0,
mod_make_list(Length, List)
;
mod_length(List, 0, Length).
mod_make_list(0, []):-
!.
mod_make_list(N, [_| Tail]):-
M is N-1,
mod_make_list(M, Tail).
mod_length([], Length, Length).
mod_length([_| Tail], Acc, Length) :-
Acc2 is Acc + 1,
mod_length(Tail, Acc2, Length).

View File

@@ -0,0 +1,24 @@
:- object(object).
:- public(length/2).
length(List, Length) :-
integer(Length) ->
Length >= 0,
make_list(Length, List)
;
length(List, 0, Length).
make_list(0, []):-
!.
make_list(N, [_| Tail]):-
M is N-1,
make_list(M, Tail).
length([], Length, Length).
length([_| Tail], Acc, Length) :-
Acc2 is Acc + 1,
length(Tail, Acc2, Length).
:- end_object.

View File

@@ -0,0 +1,22 @@
my_length(List, Length) :-
integer(Length) ->
Length >= 0,
my_make_list(Length, List)
;
my_length(List, 0, Length).
my_make_list(0, []):-
!.
my_make_list(N, [_| Tail]):-
M is N-1,
my_make_list(M, Tail).
my_length([], Length, Length).
my_length([_| Tail], Acc, Length) :-
Acc2 is Acc + 1,
my_length(Tail, Acc2, Length).