Benches macOS

5

measure time in applescript

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

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

5 replies

Sign in or join Beachball Help to reply.

  1. 0

    osimp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  2. 0

    Strontium90

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Why not use do shell script and the date command and calculate time in seconds?

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

    --do stuff here

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

    set timeItTook to finishTime - startTime

    You can do additional math to calculate minutes if your processes take that long.

  3. 0

    red_menace

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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>

  4. 0

    Camelot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  5. 0

    fidel-castro

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    thank you camelot