02.12.2012 19:50:00 • Categories: Applescript • Tags: Applescript
AppleScript: optimizing loops
(* Loop optimizing Example 1 *)set MyList to {} repeat 10000 times set the end of MyList to " hello from the loop" end repeat return MyList as text -- 5.63s
set MyString to "" repeat 10000 times set MyString to MyString & " hello from the loop" end repeat -- 3.71s
(* Loop optimizing Example 2 for complete overview. * For speed testing you have to split this example in two separate files *)(*
- slow version *) --create a list to work with set MyList to {}
--put 5000 elements in the list for us to walk through for testing repeat with i from 0 to 5000 set the end of MyList to i end repeat
--now walk the list again and add up all the number set MyTotal to 0 repeat with ThisValue in MyList set MyTotal to MyTotal + ThisValue end repeat
--return MyTotal --2.87s
(*
- fast version *) --create a global scope list to work with property MyGlobalList : {}
--make sure it's empty as these are persistent between runs set MyGlobalList to {}
--now work with a reference to that global list set MyList2 to a reference to MyGlobalList
--put 5000 elements in the list for us to walk through for testing repeat with i from 0 to 5000 set the end of MyList2 to i end repeat
--now walk the list again and add up all the number set MyTotal2 to 0 repeat with ThisValue in MyList2 set MyTotal2 to MyTotal2 + ThisValue end repeat
--return MyTotal2 --0.09s !!!