Is the second of these solutions more efficient?

50 views Asked by At

Wondering if as a practical matter the post-compilation of the second solution below is more efficient .

first solution :

write_data([]).
write_data([_]) :-
    write('LAST!'),nl.
write_data([X|Rest]) :-
    Rest = [_|_],
    write(X),nl,
    write_data(Rest).

second solution :

write_data([]).
write_data([H|T]) :-
    write_data(T, H).

write_data([], _) :-
    write('LAST!'),nl.
write_data([H|T], X) :-
    write(X),nl,
    write_data(T, H).

some swipl magic :

?- [library(vm)] .

?- vm_list(write_data) , halt.

first solution swipl magic ::

========================================================================
write_data/1
========================================================================
   0 s_virgin
   1 i_exit
----------------------------------------
clause 1 (<clause>(0xb002c300)):
----------------------------------------
   0 h_nil
   1 i_exitfact
----------------------------------------
clause 2 (<clause>(0xb017da50)):
----------------------------------------
   0 h_list
   1 h_void
   2 h_nil
   3 h_pop
   4 i_enter
   5 b_atom('LAST!')
   7 i_call(write/1)
   9 i_depart(nl/0)
  11 i_exit
----------------------------------------
clause 3 (<clause>(0xb002bc80)):
----------------------------------------
   0 h_list_ff(1,2)
   3 i_enter
   4 b_unify_var(2)
   6 h_list
   7 h_pop
   8 b_unify_exit
   9 b_var1
  10 i_call(write/1)
  12 i_call(nl/0)
  14 b_var2
  15 i_depart(write_data/1)
  17 i_exit

second solution swipl magic ::

========================================================================
write_data/1
========================================================================
   0 s_virgin
   1 i_exit
----------------------------------------
clause 1 (<clause>(0xb272c300)):
----------------------------------------
   0 h_nil
   1 i_exitfact
----------------------------------------
clause 2 (<clause>(0xb27288c0)):
----------------------------------------
   0 h_list_ff(1,2)
   3 i_enter
   4 b_var2
   5 b_var1
   6 i_depart(write_data/2)
   8 i_exit
========================================================================
write_data/2
========================================================================
   0 s_virgin
   1 i_exit
----------------------------------------
clause 1 (<clause>(0xb2728920)):
----------------------------------------
   0 h_nil
   1 i_enter
   2 b_atom('LAST!')
   4 i_call(write/1)
   6 i_depart(nl/0)
   8 i_exit
----------------------------------------
clause 2 (<clause>(0xb272bc80)):
----------------------------------------
   0 h_list_ff(2,3)
   3 i_enter
   4 b_var1
   5 i_call(write/1)
   7 i_call(nl/0)
   9 b_var(3)
  11 b_var2
  12 i_depart(write_data/2)
  14 i_exit

Upon submission of the above stack overflow blocked with message "It looks like your post is mostly code; please add some more details." . HRM . Too much code in a question about computer programming ? (skratches virtual head) . Should I add some verbiage I heard about the new revolution "Bunch-Oriented" programming whilst around the water-cooler ?

0

There are 0 answers