Advanced Scripts Execution Timing.

Thanks Shane. That works great.

I rewrote my script from post 16 to implement two changes: the script now uses CACurrentMediaTime(), and the results are returned as milliseconds if the timing result is less than a second and as seconds otherwise.

In testing, I happened to notice one issue. If the test code creates or manipulates a large list, the timing results can be inaccurate. A workaround is to place the test code in a handler.

I ran some simple tests comparing the results returned by this script and by Script Geek, and the results were within a few milliseconds of one another. Just in general, it’s best to use Script Geek, which can be found at:

https://macosxautomation.com/applescript/apps/Script_Geek.html

use framework "Foundation"
use scripting additions

-- untimed code

-- start time
set startTime to current application's CACurrentMediaTime()

-- timed code

-- elapsed time
set elapsedTime to (current application's CACurrentMediaTime()) - startTime
set numberFormatter to current application's NSNumberFormatter's new()
if elapsedTime > 1 then
	numberFormatter's setFormat:"0.000"
	set elapsedTime to ((numberFormatter's stringFromNumber:elapsedTime) as text) & " seconds"
else
	(numberFormatter's setFormat:"0")
	set elapsedTime to ((numberFormatter's stringFromNumber:(elapsedTime * 1000)) as text) & " milliseconds"
end if

-- result
elapsedTime