Home > Archive > Prolog > February 2007 > sub_atom implementation
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
sub_atom implementation
|
|
| Mauro Di Nuzzo 2007-02-13, 7:07 pm |
| Hi All!
I am using SWI Prolog. I have two issues regarding sub_atom/5 predicate.
First, is it convenient to use it or it is better to use difference list (in
terms of speed).
Does it depends on the implementation I use?
Second, as a declarative language Prolog allow to "say what you want" and
not "how".
Is it possible to say sub_atom/5 that I want first longer sub atoms without
tailoring a new predicate?
Thanks in advance.
M
| |
| Jan Wielemaker 2007-02-13, 7:07 pm |
| On 2007-02-13, Mauro Di Nuzzo <picorna@inwind.it> wrote:
> Hi All!
> I am using SWI Prolog. I have two issues regarding sub_atom/5 predicate.
>
> First, is it convenient to use it or it is better to use difference list (in
> terms of speed).
> Does it depends on the implementation I use?
Of course there may be good and bad implementations. The SWI-Prolog one
is fairly optimal given the architecture of the system. Regardless of
the architecture of the Prolog system concerned however, creating atoms
is relatively expensive. They are shared resources that must be kept in
a table and be subject to garbage collection. In SWI-Prolog (and a few
more) things are one step worse as the system is multi-threaded and
atoms are shared between threads. This means synchronization is needed
to create them. Worse, atom garbage collection blocks *all* threads.
In general, most of the non-deterministic usage will be slow. Similar,
creating an atom using a simple split or atom_concat is ok. Creating
complex atoms from many parts that are splitted and appended in multiple
steps, creating lots of intermediate atoms is slow too.
In most of these cases, manipulating lists of character(-codes) using
list-processing is generally a better idea.
> Second, as a declarative language Prolog allow to "say what you want" and
> not "how".
> Is it possible to say sub_atom/5 that I want first longer sub atoms without
> tailoring a new predicate?
Write a generator over the length that works backwards. If performance
is not critical (and that is quite often), go for simple and easily
understable code. If you need lots of this and it must be fast, consider
usings lists and appropriate list processing primitives.
Cheers --- Jan
|
|
|
|
|