4

I'm writing a program where I need to see if strings match a particular pattern. Right now I've got this implemented in Prolog as a rule matchesPattern(S), with well over 20 different definition.

I end up running all the binary strings up to a certain length through the pattern checking predicate. The program is fairly slow (as Prolog often is), and since there are so many different definitions, I'd ideally like to order them so the ones most matched are earliest in the ordering, and thus matched first by Prolog, avoid backtracking as much as I can.

I'm using SWI Prolog right now, but I have access to SICStus, so I'm willing to use it or any Prolog interpreter I can get for free.

false
  • 10,264
  • 13
  • 101
  • 209
jmite
  • 8,171
  • 6
  • 40
  • 81

3 Answers3

1

SWI-Prolog has profile/3 and show_profile/2 that could help with your task.

Left factoring your pattern rules, and applying cuts, could improve the runtime, if there are common parts between patterns. Such analysis should be combined with the statistics.

CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • Profile tells which goals take the most time, but does it tell which definition of each goal takes the most time? I've used it quite a bit, I just don't know how to get it to give information relevant to the ordering. – jmite Jun 14 '12 at 17:28
  • you are right, a 'dispatch' symbol is needed. But then you could as well count yourself the calls... – CapelliC Jun 14 '12 at 18:48
-2

You shoudl think about using DCG and cuts.

whd
  • 1,819
  • 1
  • 21
  • 52
  • I'm already using cuts quite a bit, as well as DCG when it's convenient. Does DCG provide a speed boost at all? – jmite Jun 14 '12 at 16:59
-3

You should look into cuts. The prolog syntax for this is:

!
Ihmahr
  • 1,110
  • 1
  • 16
  • 25
  • I do use cuts, quite a bit. However, the effectiveness of cuts is highly dependent on the ordering of the clauses, i.e. if the last one is always the one to succeed, it doesn't matter if I cut after the other ones succeed, since I still had to try them all anyways. – jmite Jun 14 '12 at 16:58