<HTML><FONT FACE=arial,helvetica><FONT SIZE=2>Excellent explanation. I knew some of the tricks but you clearly showed a faster method. This will immediately help me in writing faster scripts.<BR>
<BR>
Philip Chumbley<BR>
<BR>
<BLOCKQUOTE TYPE=CITE style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">How to speed up MC<BR>
<BR>
All those guys that migrated from good old HyperCard to MC, were<BR>
delighted by the speed enhancement. Nevertheless, in a number of cases,<BR>
MC is slow compared to languages like Pascal or C. Scott Raney gives<BR>
some good advice about how to speed up your scripts. However, there are<BR>
some of less known tricks that can speed up your scripts amazingly.<BR>
Suppose you want to create a list consisting of a number of lines with<BR>
something put into each line. For the sake of simplicity, let us create<BR>
a list of 10000 random numbers between 1 and 1000. If your script looks<BR>
like this:<BR>
<BR>
script A<BR>
<BR>
repeat with i = 1 to 10000<BR>
put random (1000) into line i of randList<BR>
end repeat<BR>
<BR>
and are eager to know how to write a script that executes more then ten<BR>
times as fast, you should read on!<BR>
But first some questions. Do you think that the same script, but now<BR>
generating random numbers between 1 and 1000000 (one million) takes more<BR>
time? If your answer is “yes”, you’re right. It will take about twice as<BR>
much time. Do you think that’s because generating a random number<BR>
between 1 and 1000000 takes more time than generating a random number<BR>
between 1 and 1000? If your answer is “yes”, you’re wrong. It takes<BR>
about the same amount of time.<BR>
To write scripts that executes as fast as possible, you should know<BR>
something about how a computer (or MetaCard) works, how data are stored,<BR>
etc. I’m going to tell you that in a very, very simplified way. But<BR>
first, the fast script:<BR>
<BR>
script B<BR>
<BR>
repeat with i = 1 to 10000<BR>
put random (1000) & cr after randList<BR>
end repeat<BR>
delete last char of theList<BR>
<BR>
Script B yields exactly the same result as script A, but is more then<BR>
ten times as fast. If we wanted to generate random numbers between 1 and<BR>
1000000, it is even more then 20 times as fast.<BR>
<BR>
Here is the reason why.<BR>
Data are stored in memory. Each piece of data has a particular length.<BR>
For example, in script A, the 75th call to random (1000) may yield 234,<BR>
which is three characters long. This piece of data had to be put after<BR>
the CR (carriage return) at the end of line number 74. But where in<BR>
memory is that? The computer can only figure that out by counting the<BR>
number of CR’s from the start of ‘randList’. And that takes time. And<BR>
each call to random (1000) the computer (the poor thing) starts counting<BR>
the CR’s again.<BR>
Script B does not have this drawback. The computer does not have to<BR>
bother about counting CR’s, but can just put the result of random (1000)<BR>
after the last char of ‘randList’. In script B a CR is placed after this<BR>
line, to warrant that each new result of random (1000) appears on a new<BR>
line. Finally, the last cr is deleted, to ensure that the results of<BR>
script A and B are exactly the same.<BR>
To count the number of CR’s in script A, the computer has to walk<BR>
through all characters of ‘randList’ and to decide whether or not the<BR>
character is a CR. If we generated random numbers between 1 and 1000000,<BR>
the computer has to walk through about twice (actually, less than twice,<BR>
because also the CR’s are characters) as much characters than in case of<BR>
generating random numbers between 1 and 1000. Hence generating random<BR>
numbers between 1 and 1000000 with script A takes about twice as much<BR>
time than generating random numbers between 1 and 1000 with script A. In<BR>
script B there is no difference of course.<BR>
If you understand the principle, you can easily imagine that the speed<BR>
increase will become negligible if the number of characters on a line is<BR>
very small, but very large if the mean number of characters on each line<BR>
as large.<BR>
<BR>
Moreover, you will also understand why using array variables is so fast.<BR>
Array variables are indexed. If the variable AR is an array variable,<BR>
AR[345] directly points to the correct position in memory. Hence, a<BR>
statement like ‘get AR [345]’ is very fast. On the other hand, the<BR>
statement ‘get line 345 of randList’ is much slower: again, the computer<BR>
has to count 344 CR’s, walking through the data of ‘randlist’.<BR>
<BR>
Similarly, if you want to perfom some action on each number in<BR>
‘randList’ using the script:<BR>
<BR>
script C<BR>
<BR>
repeat for each line randNumber in randList<BR>
put randNumber into temp<BR>
-- do something<BR>
end repeat<BR>
<BR>
is much faster then<BR>
<BR>
script D<BR>
<BR>
repeat with i = 1 to the number of lines of randList<BR>
put line i of randList into temp<BR>
-- do something with temp<BR>
end repeat<BR>
<BR>
Why? In script D the computer has to count CR’s to get the i’s line. In<BR>
script D, the computer ‘remembers’ where it is, getting lines; it just<BR>
proceeds through the lines and doesn’t count CR’s. Moreover, of course,<BR>
the statement ‘put randNumber into temp’ is superfluous.<BR>
<BR>
A strategy that can give good results, is:<BR>
(1) Put a list into an array, using the ‘repeat for each’ approach. Note<BR>
that you can also use items, or words.<BR>
(2) Perform actions on the content of the array.<BR>
(3) Put it back into the list, e.g. to display it in a field.<BR>
This approach is especially useful if you want to change the contents of<BR>
the list, because you cannot use ‘repeat for each’ and at the same time<BR>
change the contents (you should now understand why).<BR>
<BR>
There are more tricks, but maybe all this stuff is quite familiar to you<BR>
and offers nothing new. Please let me know if you appreciate information<BR>
like this. If not, that’s fine. It will save me time. If yes, I will<BR>
tell you the next time how to speed up calculations. And I will tell you<BR>
about some limits (for example, the largest number MC can handle). And,<BR>
for the happy few who can write C or Pascal, how to use externals or how<BR>
self-written programs in C or Pascal can be approached using Apple<BR>
events. (but don't expect me to contribute daily!) My aim is not to give<BR>
you just tricks, but to give you some understanding of the mechanisms.<BR>
If you grasp the difference between the A and B scripts, and the C and D<BR>
scripts, and when it makes a difference and when not, you can yourself<BR>
carefully inspect your scripts and improve them.<BR>
<BR>
Happy programming,<BR>
<BR>
Wil Dijkstra</BLOCKQUOTE><BR>
<BR>
</FONT></HTML>