[metapost] reading a large array

luigi scarso luigi.scarso at gmail.com
Fri Mar 10 19:14:27 CET 2017


On Fri, Mar 10, 2017 at 3:58 PM, Hans Hagen <pragma at wxs.nl> 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


More information about the metapost mailing list