[mercury-users] a question on efficient parsing of a file
Peter Wang
novalazy at gmail.com
Wed May 19 13:03:00 AEST 2010
On 2010-05-19, Ralph Becket <rafe at csse.unimelb.edu.au> wrote:
> Vladimir Gubarkov, Tuesday, 18 May 2010:
> > And third - it operates over string and current position, and my
> > experiments seem to show that this way is not as fast as operating on
> > list of chars.
>
> I'm very surprised to hear that. parsing_utils reads chars directly
> from the string source, which is just a bounds test and a memory
> reference (see string.index). List creation and manipulation is
> typically much slower.
On C backends, string.index may incur a strlen() on every call. You
should use string.unsafe_index unless you want to rely on loop invariant
hoisting.
Also if the parse_state is as before, you may end up create a new
structure per character read. This could be another reason that
traversing a list is faster -- but that is atypical.
*Not recommended*
For efficiency you can write C with Mercury syntax. Use a cursor type
that points directly at a character within the string. Advancing the
cursor is just a pointer increment. You must be careful to hold a
reference to the string, somewhere, otherwise the string may be garbage
collected. This only works for C backends.
Peter
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to: mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions: mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the users
mailing list