Compound terms and Lists in Prolog -


why compound terms preferable on lists in terms of performance? example,

matrix(row(1,2,3),row(1,2,3),row(1,2,3)) 

is preferable over

[[1,2,3],[1,2,3],[1,2,3],[1,2,3]] 

thank you!

something other (excellent) answer did not mention:

access member of list position means need traverse list. access argument of term should possible in constant time. random access term should more efficient.


short aside: can attempt make list traversal marginally faster. swi-prolog implementation of nth0_det/3 smells of desperation ;)


you might interested in this thread, esp. this summary talks lists , terms among other things.

a few rules of thumb follow.

use case:

  • if know in advance how many things have, use term.
  • if can have 0 or more of same things, use list.
  • if need look-up table, neither optimal

efficiency:

  • if want random access, use term
  • if algorithm works on singly-linked list, prolog list choice.

from last point follows: try find algorithms use linked lists, not random-access arrays. not possible, many problems have choice. classical example quick sort vs. merge sort: in prolog, merge sort quicker.

either way, first make sure right, worry efficiency. , make sure measure performance bottlenecks before starting optimize.

choosing optimal algorithm , data structure means, of course, need know both problem , available tools. not relevant problem, beauty of used "standard template library" c++ both algorithms , data structures, time complexity ("big o notation") inherent property, not implementation detail.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -