phrase vs. call performance and input constraints

14 views
Skip to first unread message

Marijn Schraagen

unread,
Nov 19, 2015, 11:05:32 AM11/19/15
to SWI-Prolog
I was doing some tests on the performance of phrase/3. The idea is to calculate all combinations of 10 np's and a vp from 5 lexical np's and 2 lexical vp's. I tested three situations: direct dcg, mediated by call/3, and mediated by phrase/3 (code and results of timing below).

The direct approach and call/3 are comparable (although call/3 is a bit slower, why?), but phrase/3 is much less efficient. This is likely caused by the checks that are performed in phrase/3, being integrity of the input and dcg_special-ness of the predicate. Given this result, I'm thinking of replacing all occurrences of phrase/3 in my code with call/3 (for a specific, computationally demanding project).

Is that a good idea? What are the constraints on the grammar predicates and the input when using call/3 (i.e., in phrase / call_dcg, what do dcg_special/1 and dcg_body/7 and call(C:H) do)?

s1 --> np0, np0, np0, np0, np0, np0, np0, np0, np0, np0, vp0.
s2
--> ph2(np), ph2(np), ph2(np), ph2(np), ph2(np), ph2(np), ph2(np), ph2(np), ph2(np), ph2(np), ph2(vp).
s3
--> ph3(np), ph3(np), ph3(np), ph3(np), ph3(np), ph3(np), ph3(np), ph3(np), ph3(np), ph3(np), ph3(vp).
np0
--> np.
vp0
--> vp.
ph2
(Ph) --> call(Ph).
ph3
(Ph) --> phrase(Ph).

np
--> [a].
np
--> [b].
np
--> [c].
np
--> [d].
np
--> [e].
vp
--> [x].
vp
--> [y].

[debug]  ?- time(aggregate_all(count, phrase(s1,L,[]), Count)).
% 102,539,081 inferences, 10.668 CPU in 10.664 seconds (100% CPU, 9611730 Lips)
Count = 19531250.

[debug]  ?- time(aggregate_all(count, phrase(s2,L,[]), Count)).
% 102,539,081 inferences, 11.194 CPU in 11.191 seconds (100% CPU, 9159933 Lips)
Count = 19531250.

[debug]  ?- time(aggregate_all(count, phrase(s3,L,[]), Count)).
% 236,816,422 inferences, 24.826 CPU in 24.818 seconds (100% CPU, 9538938 Lips)
Count = 19531250.



Reply all
Reply to author
Forward
0 new messages