The speed of MC
wouter
wouter.abraham at pi.be
Tue Apr 1 13:23:01 EST 2003
> The speed of MC
>
> * From: Wil Dijkstra
> * Subject: The speed of MC
> * Date: Mon, 31 Mar 2003 08:17:21 -0800
>
> How to speed up MC
----- snip :^))
> To write scripts that executes as fast as possible, you should know
> something about how a computer (or MetaCard) works, how data are
> stored,
> etc. I’m going to tell you that in a very, very simplified way. But
> first, the fast script:
>
> script B
>
> repeat with i = 1 to 10000
> put random (1000) & cr after randList
> end repeat
> delete last char of theList
>
> Script B yields exactly the same result as script A, but is more then
> ten times as fast. If we wanted to generate random numbers between 1
> and
> 1000000, it is even more then 20 times as fast.
>
------- snip
Very nice indeed.
And if you are really into the need for speed, script B is even a tad
faster like this :
script B
repeat 10000
put random (1000) & cr after randList
end repeat
delete last char of randList
Another speed improvement on repeat loops may be obtained in certain
cases (depending on the massiveness of the number of repeats and on the
amount and kind of data processed), by using the old technique of
"unrolling" the repeat loop (which also works on script languages).
Instead of a lengthy explanation on the overhead of the repeat loop
itself, let the the following be self-explanatory. Feed x some numbers :
on mouseUp
put fld "orig" into x
put "calculating....." into fld "result"
put "" into theResult
put 0 into a
put 0 into b
put 0 into c
put the long seconds into Time1
repeat 10000
add (x + x + x + x + x + x + x + x + x + x) to a
end repeat
put the long seconds into Time2
repeat with i = 1 to 10000
add (x + x + x + x + x + x + x + x + x + x) to b
end repeat
put the long seconds into Time3
repeat 500
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
add (x + x + x + x + x + x + x + x + x + x) to c
end repeat
put the long seconds into Time4
put "• add (x + x + x + x + x + x + x + x + x +x) to xx" & cr after
theResult
put " --> " & (a + b + c) / 3 & cr after theResult
put Time2 - Time1 & " --> repeat 10000 times" & cr after theResult
put Time3 - Time2 & " --> repeat with i = 1 to 10000 times" & cr
after theResult
put Time4 - Time3 & " --> repeat unrolled to 500 times" & cr after
theResult
put "" into a
put "" into b
put "" into c
put the long seconds into Time1
repeat 100000
add x to a
end repeat
put the long seconds into Time2
repeat with i = 1 to 100000
add x to b
end repeat
put the long seconds into Time3
repeat 5000
add x to c
add x to c
add x to c
add x to c
add x to c
add x to c
add x to c
add x to c
add x to c
add x to c
add x to c
add x to c
add x to c
add x to c
add x to c
add x to c
add x to c
add x to c
add x to c
add x to c
end repeat
put the long seconds into Time4
put "• add x to xx" & cr after theResult
put " --> " & (a + b + c) / 3 & cr after theResult
put Time2 - Time1 & " --> repeat 100000 times" & cr after theResult
put Time3 - Time2 & " --> repeat with i = 1 to 100000" & cr after
theResult
put Time4 - Time3 & " --> repeat unrolled to 5000 times" after
theResult
put theResult into fld "result"
end mouseUp
While you are at it, you can try the difference in speed in massive
repeat loops between:
add x to a
and
put a + x into a
For those who like copy - pasting :
on mouseUp
put fld "orig" into x
put "calculating....." into fld "result"
put "" into theResult
put 0 into a
put 0 into b
put 0 into c
put the long seconds into Time1
repeat 10000
put a + (x + x + x + x + x + x + x + x + x + x) into a
end repeat
put the long seconds into Time2
repeat with i = 1 to 10000
put b + (x + x + x + x + x + x + x + x + x + x) into b
end repeat
put the long seconds into Time3
repeat 500
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
put c + (x + x + x + x + x + x + x + x + x + x) into c
end repeat
put the long seconds into Time4
put "• put xx + (x + x + x + x + x + x + x + x + x +x) into xx" &
cr into theResult
put " --> " & (a + b + c) / 3 & cr after theResult
put Time2 - Time1 & " --> repeat 10000 times" & cr after theResult
put Time3 - Time2 & " --> repeat with i = 1 to 10000 times" & cr
after theResult
put Time4 - Time3 & " --> repeat unrolled to 500 times" & cr after
theResult
put 0 into a
put 0 into b
put 0 into c
put the long seconds into Time1
repeat 100000
put a + x into a
end repeat
put the long seconds into Time2
repeat with i = 1 to 100000
put b + x into b
end repeat
put the long seconds into Time3
repeat 5000
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
put c + x into c
end repeat
put the long seconds into Time4
put "• put xx + x into xx" & cr after theResult
put " --> " & (a + b + c) / 3 & cr after theResult
put Time2 - Time1 & " --> repeat 100000 times" & cr after theResult
put Time3 - Time2 & " --> repeat with i = 1 to 100000" & cr after
theResult
put Time4 - Time3 & " --> repeat unrolled to 5000 times" after
theResult
put theResult into fld "result"
end mouseUp
Have a nice whatever.
WA
More information about the metacard
mailing list