I reported AppleScript slowdown issue on macOS 11 + M1 Mac mini.
And it was fixed on macOS 12 as everybody know.
I didn’t install Sonoma beta yet.
I hope Sonoma Beta testers to write efficient report to Apple.
–Which machine?
I selected some sample code and compare the result of various machines.
–How measure it?
Measure each script by using Shane’s Script Geek.
–What script do you use?
On macOS 11, communication with GUI apps are not so slow, Cocoa scripting and random number generation was too slow.
So, I selected some scripts to measure execuion speed.
They are a kind of time consumer scripts. It takes a long time to execute.
And one script takes about 5 minutes to calculate the most far place from nearest station (700 points x 8,000 stations).
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
script spdPerm
property permutations : missing value
end script
on run
--Test by MacBook Pro 2012 Retina Core i7 2.6GHz
--set theList to {"A", "T", "G", "C"} --0.01 secs (4 digits) 24 items.
--set theList to {"A", "T", "G", "C", "1"} --0.05 secs (5 digits) 120 items.
--set theList to {"A", "T", "G", "C", "1", "2"} --0.21secs (6 digits) 720 items.
set theList to {"A", "T", "G", "C", "1", "2", "3"} --1.44 secs (7 digits) 5,040 items.
--set theList to {"A", "T", "G", "C", "1", "2", "3", "4"} --11.54 secs (8 digits) 40,320 items.
--set theList to {"A", "T", "G", "C", "1", "2", "3", "4", "5"} --107.27 secs (9 digits) 362,880 items.
set aRes to permute(theList) of me
return aRes
end run
on permute(theList as list)
set theArray to current application's NSMutableArray's arrayWithArray:theList
set (permutations of spdPerm) to current application's NSMutableArray's array()
prmt(theArray, 0, (count theList) - 1)
--Return AppleScript string list
set aFinishArray to current application's NSMutableArray's new()
set anEnum to (permutations of spdPerm)'s objectEnumerator()
repeat
set aValue to anEnum's nextObject()
if aValue = missing value then exit repeat
set aStr to aValue's componentsJoinedByString:""
(aFinishArray's addObject:aStr)
end repeat
return aFinishArray as list
end permute
on prmt(theArray, theStart as number, theEnd as number)
if (theStart = theEnd) then
(permutations of spdPerm)'s addObject:theArray
else
repeat with x from theStart to theEnd
set theCopy to theArray's mutableCopy()
--swap
if (x > theStart) then (theCopy's exchangeObjectAtIndex:theStart withObjectAtIndex:x)
prmt(theCopy, theStart + 1, theEnd)
end repeat
end if
end prmt
use AppleScript version "2.4"
use framework "Foundation"
use framework "GameplayKit" -- requires macOS 10.12
use scripting additions
script spd
property aList : {}
property aRes : {}
property bRes : {}
end script
set (aRes of spd) to {}
set (bRes of spd) to {}
--テスト用データリストの作成(30万アイテム)
set (aList of spd) to makeShuffledSequantialNumList(1, 300000) of me
--昇順ソートの時間計測
set a1Dat to current application's NSDate's timeIntervalSinceReferenceDate()
set (aRes of spd) to sort1DList_ascOrder_((aList of spd), true)
set b1Dat to current application's NSDate's timeIntervalSinceReferenceDate()
set c1Dat to b1Dat - a1Dat
--降順ソートの時間計測
set a2Dat to current application's NSDate's timeIntervalSinceReferenceDate()
set (bRes of spd) to sort1DList_ascOrder_((aList of spd), false)
set b2Dat to current application's NSDate's timeIntervalSinceReferenceDate()
set c2Dat to b2Dat - a2Dat
return {c1Dat, c2Dat}
--> {1, 1}
--1D Listをsort / ascOrderがtrueだと昇順ソート、falseだと降順ソート
on sort1DList:theList ascOrder:aBool
set aDdesc to current application's NSSortDescriptor's sortDescriptorWithKey:"self" ascending:aBool selector:"compare:"
set theArray to current application's NSArray's arrayWithArray:theList
return (theArray's sortedArrayUsingDescriptors:{aDdesc}) as list
end sort1DList:ascOrder:
on makeShuffledSequantialNumList(fromNum, toNum)
script spdL
property nList : {}
end script
set (nList of spdL) to {}
repeat with i from fromNum to toNum by 1
set the end of (nList of spdL) to i
end repeat
set anArray to current application's NSArray's arrayWithArray:(nList of spdL)
set newArray to anArray's shuffledArray() as list -- requires macOS 10.12
return newArray
end makeShuffledSequantialNumList
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
repeat 10000 times
random number from 1 to 99999999
end repeat