Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

measure time in applescript

hi,

is it possible in general to measure the execution time of parts of my script ?

If yes, could you give me a keyword ?
My search for "applescript timer" and "applescript time" was without luck so far.


i guess i could read system-clock store it somehow reformated into an int at start and do the same at the end. doing some math with both int values and done...but i hope there is a more clever method to realize timers

any ideas ?
best regards
fidel

MacBookPro 15,4" 1,83 GHz; Mac Pro 2,66 Ghz; Macbook 2,0 GHz, Mac OS X (10.5.2)

Posted on Mar 21, 2008 11:09 AM

Reply
Question marked as Best reply

Posted on Mar 21, 2008 4:17 PM

If you were using a PPC computer, there's a scripting addition ("the ticks" from Jon's Commands) that can be used pretty easily to surround a portion of a script and calculate the execution time from beginning to end of that script portion within a few "ticks" (1/60 of a second). But for Intel MAC's, the script addition doesn't work. Shareware called "Extra Suites" could also do this (but only with PPC). The developer told me he's working on an Intel version, but I haven't seen it yet.

I have written code that uses the system clock, but it's a little messy looking and is good only to the nearest second. Here's what it looks like:

------

set T1 to minutes of (current date)
set T1s to seconds of (current date)

**Portion of script to be timed goes here**

set T2 to minutes of (current date)
set T2s to seconds of (current date)
set TT_ to ((T2 * 60) + T2s) - ((T1 * 60) + T1s)

------

TT_ is the execution time of the script portion, in seconds. It may include some relatively small errors. Put in "delay 5 or delay 6.2 or whatever" in place of "portion of script to be timed goes here" to test. You'll see that the result is to the nearest second.
5 replies
Question marked as Best reply

Mar 21, 2008 4:17 PM in response to fidel-castro

If you were using a PPC computer, there's a scripting addition ("the ticks" from Jon's Commands) that can be used pretty easily to surround a portion of a script and calculate the execution time from beginning to end of that script portion within a few "ticks" (1/60 of a second). But for Intel MAC's, the script addition doesn't work. Shareware called "Extra Suites" could also do this (but only with PPC). The developer told me he's working on an Intel version, but I haven't seen it yet.

I have written code that uses the system clock, but it's a little messy looking and is good only to the nearest second. Here's what it looks like:

------

set T1 to minutes of (current date)
set T1s to seconds of (current date)

**Portion of script to be timed goes here**

set T2 to minutes of (current date)
set T2s to seconds of (current date)
set TT_ to ((T2 * 60) + T2s) - ((T1 * 60) + T1s)

------

TT_ is the execution time of the script portion, in seconds. It may include some relatively small errors. Put in "delay 5 or delay 6.2 or whatever" in place of "portion of script to be timed goes here" to test. You'll see that the result is to the nearest second.

Mar 21, 2008 5:00 PM in response to fidel-castro

You can also directly subtract the start date from the end date, then the only thing left to do is format your elapsed time into an appropriate text string (you are still limited to increments of one second, though):

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #FFDDFF;
overflow: auto;"
title="this text can be pasted into the Script Editor">
on run -- example
set startTime to (current date)
display dialog "Wait for a few moments before clicking \"OK\" for elapsed time..." with title (startTime as text)
set EndTime to (current date)
display dialog "Start: " & (startTime as text) & return & "End: " & (EndTime as text) & return & return & "Elapsed: " & TimeToText(EndTime - startTime) with title "Elapsed Time"
end run


on TimeToText(TheTime)
(*
converts a number to a time string
parameters - TheTime [integer]: the time in seconds
returns [text]: the time in the format hh:mm:ss
*)
if (class of TheTime) as text is "integer" then
set TimeString to 1000000 + 10000 * (TheTime mod days div hours) -- hours
set TimeString to TimeString + 100 * (TheTime mod hours div minutes) -- minutes
set TimeString to (TimeString + (TheTime mod minutes)) as text -- seconds
tell TimeString to set TheTime to (text -6 thru -5) & ":" & (text -4 thru -3) & ":" & (text -2 thru -1)
end if
return TheTime
end TimeToText
</pre>

Mar 22, 2008 6:33 AM in response to Strontium90

set startTime to do shell script "date +%s"


There's no need to revert to shell scripts for this. get current date will return a date object that's easily coerced to seconds, but adds additional features. It also doesn't have the overhead of do shell script

set startTime to (get current date)
-- your process goes here
set endTime to (get current date)
set duration to endTime - startTime
display dialog "Process took " & duration & " seconds."


Now that you're dealing with AppleScript date objects you can manipulate them however you like. e.g.:

set hrs to duration div hours
set mins to duration div minutes
set secs to duration mod minutes
display dialog "Process took " & hrs &" hours, " & mins & " minutes and " & secs & " seconds."


The granularity of get current date is one second, so you can't use this technique for time intervals of less than one second, but for many purposes it is sufficient.

measure time in applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.