TL; DR: Buona idea ! L'accelerazione sembra essere limitata a ~ 20% (per la maggior parte delle dimensioni delle liste).
In questa risposta, mettiamo a confronto tre diversi predicati che differiscono per quanto riguarda il riutilizzo @
-come dati:
list_tails([], [[]]). % (1) like `tails/2` given by the OP ...
list_tails([E|Es], [[E|Es]|Ess]) :- % ....... but with a better name :-)
list_tails(Es, Ess).
list_sfxs1(Es, [Es|Ess]) :- % (2) "re-use, mutual recursion"
aux_list_sfxs1(Es, Ess). % "sfxs" is short for "suffixes"
aux_list_sfxs1([], []).
aux_list_sfxs1([_|Es], Ess) :-
list_sfxs1(Es, Ess).
list_sfxs2([], [[]]). % (3) "re-use, direct recursion"
list_sfxs2(Es0, [Es0|Ess]) :-
Es0 = [_|Es],
list_sfxs2(Es, Ess).
Per misurare runtime, usiamo il seguente codice:
:-( dif (D, sicstus), current_prolog_flag (dialect ,D)
; use_module (library(between))
).
run_benchs(P_2s, P_2, L, N, T_ms) :-
between (1, 6, I),
L is 10 ^ I,
N is 10^(8-I),
length (Xs, L),
member (P_2, P_2s),
garbage_collect ,
call_walltime(run_bench_core(P_2,Xs,N), T_ms).
run_bench_core(P_2, Xs, N) :-
between(1, N, _),
call (P_2, Xs, _),
false .
run_bench_core(_, _, _).
Per misurare wall-time , utilizziamo call_ walltime /2
- una variazione di call_time/2
:
call_walltime(G, T_ms) :-
statistics (walltime , [T0|_]),
G,
statistics(walltime, [T1|_]),
T_ms is T1 - T0.
Mettiamola variazioni codice sopra alla prova ...
- ... attraverso lista diverse lunghezze
L
...
- ... e funzionante ogni prova diverse volte
N
(per migliore precisione).
In primo luogo, usiamo swi-prolog versione 7.3.14 (64 bit):
?- run_benchs([list_sfxs1,list_sfxs2,list_tails], P_2, L, N, T_ms).
P_2 = list_sfxs1, L*N = 10*10000000, T_ms = 7925
; P_2 = list_sfxs2, L*N = 10*10000000, T_ms = 7524
; P_2 = list_tails, L*N = 10*10000000, T_ms = 6936
;
P_2 = list_sfxs1, L*N = 100*1000000, T_ms = 6502
; P_2 = list_sfxs2, L*N = 100*1000000, T_ms = 5861
; P_2 = list_tails, L*N = 100*1000000, T_ms = 5618
;
P_2 = list_sfxs1, L*N = 1000*100000, T_ms = 6434
; P_2 = list_sfxs2, L*N = 1000*100000, T_ms = 5817
; P_2 = list_tails, L*N = 1000*100000, T_ms = 9916
;
P_2 = list_sfxs1, L*N = 10000*10000, T_ms = 6328
; P_2 = list_sfxs2, L*N = 10000*10000, T_ms = 5688
; P_2 = list_tails, L*N = 10000*10000, T_ms = 9442
;
P_2 = list_sfxs1, L*N = 100000*1000, T_ms = 10255
; P_2 = list_sfxs2, L*N = 100000*1000, T_ms = 10296
; P_2 = list_tails, L*N = 100000*1000, T_ms = 14592
;
P_2 = list_sfxs1, L*N = 1000000*100, T_ms = 6955
; P_2 = list_sfxs2, L*N = 1000000*100, T_ms = 6534
; P_2 = list_tails, L*N = 1000000*100, T_ms = 9738.
Poi, si ripete la query precedente utilizzando sicstus-prolog versione 4.3.2 (64-bit):
?- run_benchs([list_sfxs1,list_sfxs2,list_tails], P_2, L, N, T_ms).
P_2 = list_sfxs1, L*N = 10*10000000, T_ms = 1580
; P_2 = list_sfxs2, L*N = 10*10000000, T_ms = 1610
; P_2 = list_tails, L*N = 10*10000000, T_ms = 1580
;
P_2 = list_sfxs1, L*N = 100*1000000, T_ms = 710
; P_2 = list_sfxs2, L*N = 100*1000000, T_ms = 750
; P_2 = list_tails, L*N = 100*1000000, T_ms = 840
;
P_2 = list_sfxs1, L*N = 1000*100000, T_ms = 650
; P_2 = list_sfxs2, L*N = 1000*100000, T_ms = 660
; P_2 = list_tails, L*N = 1000*100000, T_ms = 740
;
P_2 = list_sfxs1, L*N = 10000*10000, T_ms = 620
; P_2 = list_sfxs2, L*N = 10000*10000, T_ms = 650
; P_2 = list_tails, L*N = 10000*10000, T_ms = 740
;
P_2 = list_sfxs1, L*N = 100000*1000, T_ms = 670
; P_2 = list_sfxs2, L*N = 100000*1000, T_ms = 650
; P_2 = list_tails, L*N = 100000*1000, T_ms = 750
;
P_2 = list_sfxs1, L*N = 1000000*100, T_ms = 12610
; P_2 = list_sfxs2, L*N = 1000000*100, T_ms = 12560
; P_2 = list_tails, L*N = 1000000*100, T_ms = 33460.
Sommario:
- Il alias-thingy lattina e fa migliorare le prestazioni in modo significativo.
- Nei test di cui sopra, il SICStus Prolog jit dà 10X aumento di velocità, rispetto a SWI-Prolog!
nota 1: Perché i bravata di mettere (@)/2
nella testa regola? Per finire con codice non idiomatic Prolog?
Nota 2: Siamo interessati al runtime totale. Perché? Perché i costi di raccolta rifiuti mostrano con dimensioni di dati più grandi!
Nota 3: La sequenza di risposta è stata post-elaborata per motivi di leggibilità.
Nota 4: Disponibile dal release 4.3.0. Le attuali architetture di destinazione includono IA-32 e AMD64.
In prolog è possibile evitare lo pseudonimo. Basta usare le code (L, [L | TA]): - L = [_ | T], ... '. – Bakuriu
Sono d'accordo, ma sarebbe bello se ciò fosse possibile nella testa. (So che sono fastidioso: S) –