Re: A question on technique
Re: A question on technique
- Subject: Re: A question on technique
- From: Shane Stanley <email@hidden>
- Date: Tue, 17 Mar 2015 14:18:38 +1100
On 17 Mar 2015, at 1:23 pm, Olof Hellman <email@hidden> wrote:
>
> And here’s a third, using TextEdit app (fairly universally available) as your search engine. This will scale well for performance
It might scale well, but it's still much, much slower.
Here's a script that times several of the methods posted. It uses a list of 300 items, with only the last one matching. It requires Yosemite for the ASObjC version and timing, and it should be run as an applet, because editors skew results (as, probably, does using a large number of iterations). Results are in milliseconds.
The first thing you'll see is that results vary. But typically the osax/regex method is fastest, followed closely by the ASObjC and TIDs methods. Looping through is slower, and calling in an app like TextEdit blows out the times massively.
use AppleScript version "2.4" -- requires 10.10
use scripting additions -- requires Satimage.osax
use framework "Foundation"
property Codes : {"9420", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "942C", "9470", "94ae"
, "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "9470", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "942C", "9470", "94ae", "94d0", "942f", "9471"}
property Mnemonics : {"RCL", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "ED
M", "RfW", "ENM", "ReW", "EOC", "EDM", "RfW", "ENM", "ReW", "EOC", "RfX"}
property codesAndMnemonics : missing value -- will hold dictionary
property lookupTable : "" -- will hold string for later methods
set iterationCount to (text returned of (display dialog "Number of iterations:" default answer "1")) as integer
-- create dictionary
set my codesAndMnemonics to current application's NSDictionary's dictionaryWithObjects:Mnemonics forKeys:Codes
-- time ASObjC method
set time1 to current application's NSDate's |date|()
repeat iterationCount times
MnemonicOf("9471") -- ASObjC handler
end repeat
set x to ((time1's timeIntervalSinceNow()) * -1000)
-- time Axel's method
set time1 to current application's NSDate's |date|()
repeat iterationCount times
MnemonicOfAxel("9471") -- Axel's version
end repeat
set a to ((time1's timeIntervalSinceNow()) * -1000)
-- create tab/return-delimited string for Chris's methods
set theList to {}
repeat with i from 1 to count of Codes
set end of theList to item i of Codes & tab & item i of Mnemonics
end repeat
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return}
set my lookupTable to theList as text
set AppleScript's text item delimiters to saveTID
-- time TIDs method
set time1 to current application's NSDate's |date|()
repeat iterationCount times
lookupMnemonic("9471") -- Chris's TIDs handler
end repeat
set z to ((time1's timeIntervalSinceNow()) * -1000)
-- call Satimage command to force loading, otherwise time is much longer
try
find text "abc" using "\\1" in lookupTable with regexp and string result without case sensitive
end try
-- time Satimage/regex method
set time1 to current application's NSDate's |date|()
repeat iterationCount times
lookupMnemonicSIO("9471") -- Chris's Satimage handler
end repeat
set y to ((time1's timeIntervalSinceNow()) * -1000)
-- build TextEdit doc for Olof's method
set allMnemonics to ""
repeat with n from 1 to count Codes
set allMnemonics to allMnemonics & item n of Codes & ", " & item n of Mnemonics & return
end repeat
tell application "TextEdit"
if not (exists document "Mnemonics") then
make new document with properties {name:"Mnemonics"}
end if
set text of document "Mnemonics" to allMnemonics
end tell
-- time TextEdit doc method
set time1 to current application's NSDate's |date|()
repeat iterationCount times
MnemonicOfTextEdit("9471") -- TextEdit handler
end repeat
set b to ((time1's timeIntervalSinceNow()) * -1000)
-- show results
display dialog "Results: " & return & "ASObjC: " & x & return & "Satimage/regex: " & y & return & "TIDs: " & z & return & "Axel's loop: " & a & return & "TextEdit: " & b
on MnemonicOf(aCode)
set returnResult to codesAndMnemonics's objectForKey:aCode
if returnResult = missing value then
-- whatever
return false
else
return returnResult as text
end if
end MnemonicOf
-- Satimage.osax
on lookupMnemonicSIO(_mnemoic)
try
set _mnemoic to "^" & _mnemoic & "\\t(.+$)"
find text _mnemoic using "\\1" in lookupTable with regexp and string result without case sensitive
on error
return false
end try
end lookupMnemonicSIO
-- use TIDs
on lookupMnemonic(_mnemoic)
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {_mnemoic & tab}}
text items of lookupTable
set _output to paragraph 1 of text item 2 of lookupTable
set AppleScript's text item delimiters to oldTIDS
return _output
end lookupMnemonic
on MnemonicOfAxel(aCode)
local k
repeat with k from 1 to length of my Codes
if item k of my Codes is aCode then return item k of my Mnemonics
end repeat
return missing value
end MnemonicOfAxel
on MnemonicOfTextEdit(aCode)
tell application "TextEdit"
set theParagraphs to every paragraph of document "Mnemonics" where it begins with aCode
end tell
return ((characters 7 thru -2 of item 1 of theParagraphs) as text)
end MnemonicOfTextEdit
--
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden