Speeding up array initialization

jbv jbv.silences at Club-Internet.fr
Sun Nov 23 15:47:26 EST 2003


Geoff,

It's a bit complicated to explain...
It's actually a piece of software for a psychology lab experiment dealing
with the way operators pay attention to complex processes on screen
and how they react to unexpected situations.
In short, various things happen / change / evolve on the screen according
to some rules and user actions. Various data need to be recorded / processed
in realtime in variables of various sizes (mainly 100*100, 150*150, 200*200
and 400*400). Under some special circumstances, the situation can change
abruptly and the user has to react / find solutions as soon as he can...
This is when the variables need to be re-initialized as soon as possible
(with values determined by previous situations, so it can be true or
false 
or any numeric values), and especially when the user CAN'T be asked to wait,
coz the speed of his response is one of the keys of the task...

If I use vars made of items and lines, it's easy (and fast) to re-initialize

them in less than 1 tick (see my first post). But as for processing data,
for instance :
        repeat with j=84 to 320
            repeat with i=12 to 57
                add 13 to item i of line j of myVar
            end repeat
        end repeat

I realized that the following line :    add 13 to myVar[j,i]
runs 25% to 30% faster than    add 13 to item i of line j of myVar
That's why I decided to use arrays instead.

But when it comes to re-initialization, it was too slow.

Fortunately, I found my solution (I've just tested it).
First I build a reference var at startup (or I pre-compute it and store it
in some hidden fld) that contains :
1,1 a
1,2 a
1,3 a
......
100,100 a

Second, when I need to initialize or re-initialize an array, I just use the
following script :
    put myRefVar into T
    replace "a" with "0" in T
    split T using return and space

This is VERY fast. I know that the most recent machines can initialize
a 100*100 array in a snap, but my app will also run on slower machines...
So speed is the key.

I hope the above is clear enough to answer your question.
Anyway, thanks to all those who helped me understand the split cmd.

Cheers,
JB


> Can we back up a moment? I'm not sure I understand what the real
> problem is here. Can you describe in more detail what you want to
> accomplish.
>
> I did a quick test on a G3 400, and initializing a 100,100 array to "a"
> took less than 20 ticks.
>
> That's fast, but even so I'd be looking for ways to be responsive to
> the user before all the data is in place. Initialize in pieces, and
> only make the user wait if he/she asks for something that hasn't yet
> been initialized.
>
> But again, what's the actual goal?
>
> regards,
>
> Geoff Canyon
> gcanyon at inspiredlogic.com
>
> On Nov 22, 2003, at 11:26 PM, jbv wrote:
>
> >
> >
> > Geoff,
> >
> >> First, remember that you don't have to initialize an array unless you
> >> have values you want in it.
> >
> > I know, but in my case, I need at least 5 to 10 arrays of 100*100
> > minimum,
> >
> > each one initialized with various values, and then re-initialized to
> > other
> > values
> > (or process parts of the data already stored in the arrays) on the fly
> > according
> > to user actions...
> > IOW I can't have the user waiting 5 times 115 ticks before he can use
> > the
> > app
> > again...
> > At first, I thought of storing these data in variables made of lines &
> > items,
> > since I had found a way to re-initialize such variables in less than 1
> > tick.
> > But I found out that processing the same data in arrays is 25% to 30%
> > faster.
> > So I was in search for a fast way to initialize arrays...
> >
> > ---------------
> >
> > As for your example of using the split command.
> > OK, I get it now. But I'm afraid it won't be of any use to me, since
> > building
> > the content of the variable to be used by split cmd might be even
> > slower
> > than
> > initializing the array with 2 nested repeat loops...
> >
> > A nice feature would be to be able to use "replace" with arrays.
> > This way, I could initialize 1 array at startup (with zeros for
> > instance),
> >
> > and then duplicate it under a different name, and use :
> >     replace 0 with myNewData in MyNewArray
> >
> >
> > Mmmh... Actually, a new idea just crossed my mind :
> > I could also build 1 variable at startup, which would contain the
> > following :
> > 1,1 a
> > 1,2 a
> > 1,3 a
> > .....
> > 100,100 a
> >
> > and then use the following script when I want to (re-)initialize an
> > array
> > :
> >     put myOriginalVar into myNewVar
> >     replace "a" with "0" in myNewVar
> >     split myNewVar using return and space
> >
> > I guess this should work pretty fast (have to test it though)...
> >
> > I could also write an external in C that would give me access to all
> > arrays & structures in C...
> >
> > Well, anyway, all these solutions are a bit tricky... I mean : there's
> > still a lot of space for improvement of arrays in MC/Rev...
> >
> > Thank you all anyway,
> > JB
> >
> >
> >
> >>
> >> In the case you listed below, what you want is something like this:
> >>
> >> 1,1 a
> >> 1,2 b
> >> 1,3 c
> >> 1,4 d
> >> 1,5 e
> >> 2,1 f
> >> 2,2 g
> >> 2,3 h
> >> 2,4 i
> >> 2,5 j
> >>
> >> In the above the separator is a space, for ease of use in email. You
> >> need to use something that is guaranteed not to be in your index or
> >> data.
> >>
> >> Given the above in a variable x,
> >>
> >> split x using return and space
> >>
> >> would give you the array you want.
> >>
> >> So now you just need a way to go from the lines you have to the
> >> information above. Something like this will work:
> >>
> >>    put empty into tNewData
> >>    put 0 into tLineCounter
> >>    repeat for each line L in tData
> >>      add 1 to tLineCounter
> >>      put 0 into tItemCounter
> >>      repeat for each item T in L
> >>        add 1 to tItemCounter
> >>        put tLineCounter,tItemCounter && T & cr after tNewData
> >>      end repeat
> >>    end repeat
> >>
> >> regards,
> >>
> >> Geoff Canyon
> >> gcanyon at inspiredlogic.com
> >>
> >> On Nov 22, 2003, at 1:49 PM, jbv wrote:
> >>
> >>>>
> >>>>
> >>>> You can change your delimiters in the split, JB.  (The second cannot
> >>>> be
> >>>> null, though.)  Build your value to be split appropriately.
> >>>>
> >>>
> >>> Well, thanks for the advice, but I've already tried the split cmd in
> >>> many
> >>> ways, but with no success...
> >>>
> >>> Here's an example :
> >>> I have a variable S made of 2 lines of 5 items each :
> >>>     line 1 :    a,b,c,d,e
> >>>     line 2 :    f,g,h,i,j
> >>>
> >>> how do I use the split cmd so that I get a 2 dimensions array S in
> >>> which :
> >>>     S[1,1] = a
> >>>     S[1,2] = b
> >>>     S[2,1] = f
> >>>     S[2,2] = g
> >>> and so on...
> >>>
> >>> Thanks,
> >>> JB
> >>>
> >>> _______________________________________________
> >>> use-revolution mailing list
> >>> use-revolution at lists.runrev.com
> >>> http://lists.runrev.com/mailman/listinfo/use-revolution
> >>>
> >>
> >> _______________________________________________
> >> use-revolution mailing list
> >> use-revolution at lists.runrev.com
> >> http://lists.runrev.com/mailman/listinfo/use-revolution
> >
> >
> > _______________________________________________
> > use-revolution mailing list
> > use-revolution at lists.runrev.com
> > http://lists.runrev.com/mailman/listinfo/use-revolution
> >
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> http://lists.runrev.com/mailman/listinfo/use-revolution


More information about the use-livecode mailing list