Re: A faster offset...
Re: A faster offset...
- Subject: Re: A faster offset...
- From: Arthur J Knapp <email@hidden>
- Date: Thu, 06 Sep 2001 11:27:43 -0400
>
Date: Thu, 06 Sep 2001 17:29:21 +1000
>
Subject: A faster offset...
>
From: David Lloyd <email@hidden>
>
A (much) faster offset...
>
>
Today, I was a little surprised by these results, comparing the timings of
>
the following two snippets, which finds offsets in strings. The first one,
>
the find offset from the standard additions, took 9 seconds to run, while
>
the second one, a routine using applescript's text item delimiters, took
>
only 0.25 seconds to run, which is 36 times faster.
>
>
For a script that calls on finding offsets a lot, this turns out to be a bit
>
of a timesaver. Perhaps it's because it's not needing to call up the osaxen
>
business? Can anyone confirm that this speed difference is the same for
>
others?
Absolutly. Traversing a large string or list, calling an osaxen command
for each item/character, can be rather slow. Consider the following:
-- k256 = A lot of memory for a small script, perhaps, but
-- very useful.
--
property k256 : run script "
set a to {}
repeat with x from 0 to 255
set a's end to ASCII character x
end
\"\" & a"
-- Handler traverse string, calling Character2Ascii().
-- We also save-set-restore the tids here.
--
on String2Ascii(str)
set asciiList to {}
set oldDelim to text item delimiters
repeat with i in str
set asciiList's end to Character2Ascii(i)
end repeat
set text item delimiters to oldDelim
return asciiList
end String2Ascii
-- Handler returning the character-set number for any
-- given character. It does not call an osax command.
-- Because we would expect a "main handler" or main
-- script body to worry about the text item delimiters,
-- this handler just sets them and forgets them.
--
on Character2Ascii(c)
set text item delimiters to "" & c
return k256's text item 1's length
end Character2Ascii
-- Handler using the ASCII number osax command
--
on UseAsciiOsax(str)
set asciiList to {}
repeat with i in str
set asciiList's end to ASCII number i
end repeat
return asciiList
end UseAsciiOsax
set text item delimiters to {""}
-- Our string to convert
--
set myString to ""
repeat 1000 times
set myString to myString & some item of k256
end repeat
set t1 to current date
set theAsciiNumbers to UseAsciiOsax(myString)
set t2 to current date
set theAsciiNumbers to String2Ascii(myString)
set t3 to current date
"UseAsciiOsax()'s time = " & (t2 - t1) & return & ,
"String2Ascii()'s time = " & (t3 - t2)
--> UseAsciiOsax()'s time = 41
-- String2Ascii()'s time = 8
If we are talking about just 4 or 5 some items to convert,
then there is not a lot of difference between UseAsciiOsax()
and String2Ascii(), but when you are proccessing large values
it helps to cut down on Scripting Addition commands.
Arthur J. Knapp
http://www.stellarvisions.com
mailto:email@hidden
Hey, check out:
http://www.latenightsw.com/freeware/JavaScriptOSA/