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.
*)




--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

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

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.

You can use ApplescriptObjC

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