Re: reading a large array

luigi scarso <[email protected]>
Newsgroups gmane.comp.tex.metapost
Message-ID <CAG5iGsCGehEyKauxhcHeLpuzhiuf8CenfmytvX5pfD_GLgtvKA@mail.gmail.com>
On Fri, Mar 10, 2017 at 3:58 PM, Hans Hagen <[email protected]> wrote:
> this runs in 3 sec on my machine which is way faster than storing at the mp
> end where at some point you hit limitations in constructing names for
> variables (keep in mind that a[i] is not an array but a hashed variable name
> constructed from a and i)
>
>

We have played a bit, and the point is that
is better to think to a[] as a tree, and lesser is the depth , the better.

For example given c and foo[]
one can  use foo[c]  to store s, but
can also declare  foo[][][][] and say
foo[c div 1000][c div 100][c div 10][c] := s ;

We don't lose items, because the last index of foo is unique, and
lookup is better
(of course we have now 3 operations, which cost).

You can experiment with the following code by Hans:


string foo[][][][];

    def set_foo(expr c, s) =
        foo[c div 1000][c div 100][c div 10][c] := s ;
    enddef ;
    def get_foo(expr c) =
        foo[c div 1000][c div 100][c div 10][c]
    enddef ;

    string s;
    numeric c ; c := 0;
    forever:
        s := readfrom "foo3.tmp";
        exitif s = EOF;
        c := c + 1 ;
        set_foo(c,s) ;
    endfor ;
    save l ; numeric l;
    for i=1 upto c :
        l := length(get_foo(c)) ;
    endfor ;



-- 
luigi
--
http://tug.org/metapost/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.