<br><br><div class="gmail_quote">On Wed, May 19, 2010 at 6:16 AM, Ralph Becket <span dir="ltr"><<a href="mailto:rafe@csse.unimelb.edu.au">rafe@csse.unimelb.edu.au</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Vladimir Gubarkov, Tuesday, 18 May 2010:<br><div class="im"></div> </blockquote><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">

>    And third - it operates over string and current position, and my<br>
>    experiments seem to show that this way is not as fast as operating on<br>
>    list of chars.<br>
<br>
</div>I'm very surprised to hear that.  parsing_utils reads chars directly<br>
from the string source, which is just a bounds test and a memory<br>
reference (see string.index).  List creation and manipulation is<br>
typically much slower.<br>
<div class="im"><br></div></blockquote><div>Ok, maybe I'm wrong, I'll definitely look more deeply into parsing_utils ones again. But what I've done was - writing next code to compare different approaches:<br>
<br>:- module fast_string_test.<br><br>:- interface.<br><br>:- import_module io.<br><br>:- pred main(io, io).<br>:- mode main(di, uo) is det.<br><br>:- implementation.<br><br>:- import_module list, string, int, parsing_utils.<br>
<br>to_char_list_(Str) = ( first_char(Str, C, Rest) -><br>              [C | to_char_list_(Rest)]<br>            ; []<br>            ).<br><br>make_list(Len, C) = (if Len = 0 then [] else [C | make_list(Len-1, C)]).<br>
<br>make_long_string(Len, Char) = S :-<br>    CharLst = make_list(Len, Char),<br>    string.to_char_list(S, CharLst).<br><br><br>:- type pos == int.<br>:- type fast_string ---><br>    fast_string(string, pos).<br><br>:- func to_fast_string(string) = fs.<br>
:- func from_fast_string(fs) = string.<br>:- func fs_length(fs) = int.<br><br>:- pred front_char(string, fs, fs). % string of len 1<br>:- mode front_char(out, in, out) is semidet.<br><br>:- pred front(string, int, fs, fs).<br>
:- mode front(out, in, in, out) is semidet.<br><br>:- type fs == fast_string.<br>    <br>to_fast_string(Str) = fast_string(Str, 0).<br>from_fast_string(FS @ fast_string(Str, Pos)) = string.unsafe_substring(Str, Pos, fs_length(FS)).<br>
<br>fs_length(fast_string(Str,Pos)) = string.length(Str) - Pos.<br><br><br>front_char(C, !FS) :- front(C, 1, !FS).<br><br>front(FrontStr, Count, F0 @ fast_string(Str, Pos), fast_string(Str, NewPos)) :-<br>    fs_length(F0) >= Count,<br>
    NewPos = Pos + Count,<br>    FrontStr = string.unsafe_substring(Str, Pos, Count).<br>    <br>%%% fast_string tst<br><br>to_char_list_fs(FS) = ( front_char(C, FS, FS1) -><br>              [C | to_char_list_fs(FS1)]<br>
            ; []<br>            ).<br><br>to_char_list_s_fs(Str) = Lst :-<br>    Fs = to_fast_string(Str),<br>    Lst = to_char_list_fs(Fs).<br><br>main(!IO) :-<br>    <br>    N = 50000000,<br>    S = make_long_string(N, 'A'),<br>
    print("made\n", !IO),<br>    %CL = to_char_list_(S):list(character),<br>    %CL = to_char_list_s_fs(S):list(string),<br>    CL = string.to_char_list(S),<br>    print("to_c_l_\n", !IO),<br>    print(length(CL):int, !IO)<br>
     <br>    %print(from_fast_string(to_fast_string("12345")), !IO)<br>    %print(to_char_list_s_fs("ABCDEFGHIJK"), !IO)<br>    .<br>    <br>(compile with <i>--optimize-constructor-last-call --infer-all</i>)<br>
<br>I believe that method used by 'CL = to_char_list_s_fs(S):list(string)' is similar to used in parsing_utils (or, maybe I'm wrong?).<br><br>But it appears, that turning string to list of chars in this way is a way slowly then by built-in string.to_char_list<br>
<br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br>
HTH,<br>
<font color="#888888">-- Ralph<br>
</font><div><div></div><div class="h5">--------------------------------------------------------------------------<br>
mercury-users mailing list<br>
Post messages to:       <a href="mailto:mercury-users@csse.unimelb.edu.au">mercury-users@csse.unimelb.edu.au</a><br>
Administrative Queries: <a href="mailto:owner-mercury-users@csse.unimelb.edu.au">owner-mercury-users@csse.unimelb.edu.au</a><br>
Subscriptions:          <a href="mailto:mercury-users-request@csse.unimelb.edu.au">mercury-users-request@csse.unimelb.edu.au</a><br>
--------------------------------------------------------------------------<br>
</div></div></blockquote></div><br>Sincerely yours,<br>Vladimir<br>