Sunday, April 23, 2017

#1 2017-02-09 08:29:42 pm

tootall8888
Member
Registered: 2015-04-21
Posts: 7

getMicroseconds() function

(*
getMicroseconds() returns an integer representing unix time in ten-thousandths of a second.
   It can be used in performance tests.  Note that getMicroseconds() takes approx. 700
  ten-thousandths of a second (or 70 milliseconds) to execute.
   *)

Applescript:




--Returns unix time in microseconds as integer.
on getMicroseconds()
   set microString to (do shell script "php -r " & quoted form of "echo microtime(true);")
   return (replace(".", "", microString) as integer)
end getMicroseconds




--Replaces searchString with replaceString inside theString:
on replace(searchString, replaceString, theString)
   set item_list to explode(theString, searchString)
   set theResult to implode(item_list, replaceString)
   return theResult -- returns a new, modified string.
end replace



-- This function separates pieces of a string into list items, using theDelimit
-- as the separator. theDelimit can be either string or list of strings.
on explode(theString, theDelimit)
   set origDelimit to AppleScript's text item delimiters
   set AppleScript's text item delimiters to theDelimit
   set theResult to every text item of theString
   set AppleScript's text item delimiters to origDelimit
   return theResult
end explode


--This function re-assembles a list of strings into a single string,
--using theDelimit as glue to reconnect each string. theDelimit must be a string.
on implode(textlist, theDelimit)
   set origDelimit to AppleScript's text item delimiters
   set AppleScript's text item delimiters to theDelimit
   set theString to (textlist as string)
   set AppleScript's text item delimiters to origDelimit
   return theString
end implode


Filed under: date, Time, Performance

Offline

 

#2 2017-02-13 03:09:21 pm

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 2603

Re: getMicroseconds() function

Not sure it's my mac or localization but the script loses precision of the floating point and it makes timing inaccurate[1]


Applescript:

repeat
   set theStart to getMicroseconds()
   set theEnd to getMicroseconds()
   
   if (theEnd - theStart) < 0 then
       return "Start time:" & tab & integerAsString(theStart) & return & "End time:" & tab & integerAsString(theEnd)
   end if
end repeat

on integerAsString(int)
   set ints to {}
   repeat until int = 0
       set beginning of ints to int mod 10 as integer
       set int to int div 10
   end repeat
   return ints as string
end integerAsString

--Returns unix time in microseconds as integer.
on getMicroseconds()
   set microString to (do shell script "php -r " & quoted form of "echo microtime(true);")
   return (replace(".", "", microString) as integer)
end getMicroseconds

--Replaces searchString with replaceString inside theString:
on replace(searchString, replaceString, theString)
   set item_list to explode(theString, searchString)
   set theResult to implode(item_list, replaceString)
   return theResult -- returns a new, modified string.
end replace

-- This function separates pieces of a string into list items, using theDelimit
-- as the separator. theDelimit can be either string or list of strings.
on explode(theString, theDelimit)
   set origDelimit to AppleScript's text item delimiters
   set AppleScript's text item delimiters to theDelimit
   set theResult to every text item of theString
   set AppleScript's text item delimiters to origDelimit
   return theResult
end explode


--This function re-assembles a list of strings into a single string,
--using theDelimit as glue to reconnect each string. theDelimit must be a string.
on implode(textlist, theDelimit)
   set origDelimit to AppleScript's text item delimiters
   set AppleScript's text item delimiters to theDelimit
   set theString to (textlist as string)
   set AppleScript's text item delimiters to origDelimit
   return theString
end implode

[1] Timing in microseconds while using a do shell script is somewhat strange. The problem is that invoking an do shell script for the first times takes long because the standard scripting addition needs to be loaded. The second time you run the script there is still the overhead of an AppleEvent even if they are nowadays fast. Still the do shell script invokes an execve() which takes a lot of time to create and kill. When the accuracy of microseconds are involved there is no timer that is accurate enough to time an handler or specific piece of code. No osax, ASObjC or IDE is capable of giving an accurate time. It's much better to repeat the process thousands or millions of times and measure the time in milliseconds and divide the outcome.

Last edited by DJ Bazzie Wazzie (2017-02-14 05:02:03 am)

Offline

 

#3 2017-03-28 09:18:46 am

robertfern
Member
Registered: 2011-11-29
Posts: 25

Re: getMicroseconds() function

You can use ApplescriptObjC

Applescript:

use AppleScript version "2.4"
use framework "Foundation"

set starttime to current application's CFAbsoluteTimeGetCurrent()

This will return time in millionth of a second from Jan 1 2001 00:00:00

Not sure what the overhead of an ApplescriptObjC call versus a Do Shell Script call
but it seems to be much less in my tests

Last edited by robertfern (2017-03-28 09:21:49 am)

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.078 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)