Just looking today at a recent exchange between Ed and Stan:
"Stockly, Ed" wrote on Thu, 17 Apr 2008 10:14:55 -0700:
(Speed testing some code by Stan):
>set midTime to the ticks
>repeat 1000 times
> set fullDeck to {}
> repeat with s in {" of H", " of C", " of S", " of D"}
> repeat with x from 1 to 13
> set c to item x of "A234567890JQK"
> if c = "0" then set c to "10"
This could be 'if x = 10'. Number comparisons are faster than text and a
little time could also be saved by not bothering to extract a character
from the string when x _is_ 10:
if (x = 10) then
set c to "10"
else
set c to character x of "A234567890JQK"
end if
> set the end of fullDeck to c & s
> end repeat
> end repeat
>end repeat
>set endTime to the ticks
>return {midTime - startTime, endTime - midTime}
>--{184, 248}
>
>-- But this version is slightly faster ({179, 172} with 1000 repeats)=A0than
>the original:
>
>repeat with x from 1 to 13
> set c to item x of {"A", "2", "3", "4", "5", "6", "7", "8", "9",
>"10", "J", "Q", "K"}
> --if c =3D "0" then set c to "10"
> set the end of fullDeck to c & s
> end repeat
>
>The speed bump comes from removing the 'if ... then'.
It would also be good to instantiate just one copy of each contributory
list before the repeats begin and to get the item from the suit list just
once before the inner repeat begins:
set fullDeck to {}
set suits to {" of H", " of C", " of S", " of D"}
set cards to {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
"Q", "K"}
repeat with s from 1 to 4
set thisSuit to item s of suits
repeat with x from 1 to 13
set the end of fullDeck to item x of cards & thisSuit
end repeat
end repeat
:)
NG
|