Re: Extracting from text [Lecture]/Re: Speeding Up Scripts 101
Re: Extracting from text [Lecture]/Re: Speeding Up Scripts 101
- Subject: Re: Extracting from text [Lecture]/Re: Speeding Up Scripts 101
- From: has <email@hidden>
- Date: Mon, 4 Mar 2002 15:00:32 +0000
Andy Wylie wrote:
>
I tested "" & notAstring awhile ago under AS 1.3.7 and found it sluggish
>
enough to put me off it though of course it's insignificant used sparingly.
while James Sentman wrote:
>
I would very much like to find a repository of script speed info.
>
>
I'll start you off with one;) I'm trying to shave some seconds off
>
the code that generates these rather large tables for a motion sensor
>
report.
-----
OK, starting with Andy's comment and working
The reason's for that sluggishness is pretty simple really:
"" & str
performs a concatenation, plus an implicit coercion (if necessary). Whereas:
str as string
performs a coercion. The only time you'd see a concatenation here is if
you're coercing a multi-item list.
Step back for a minute from your pleasant high-level language and think
about what's going on here at the lowest levels of the machine when
performing a concatenation:
===============================================
To concatenate two strings, AS has to copy that data about in memory.
e.g. If I represent the contents of strings x and y sitting in memory like so:
.......xxxxxxxx.....yyyyyyyy.......................................
and then tell AS:
x & y
the contents of both x and y are copied consecutively to a new location,
like so:
.......xxxxxxxx.....yyyyyyyy.............xxxxxxxxyyyyyyyy..........
Copying bytes takes time: the more bytes to copy, the more time it takes.
===============================================
AS hides scripters from all this low-level stuff, and rightly so - that's
one of the reasons why AS is easy to use and C is not. The downside of this
is that it makes it much easier for scripters to write inefficient
algorithms without even realising it. (Not that you can't write slow
algorithms in C too, but you've fewer excuses there for not knowing exactly
what's going on.)
--
Leading on from there, this is also why:
--------------------
set x to ""
repeat numberOfTimes times
set x to x & someString
end repeat
x
--------------------
is generally slower than:
--------------------
set y to {}
repeat numberOfTimes times
set y's end to someString
end repeat
set oldTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set y to y as string
set AppleScript's text item delimiters to oldTID
y
--------------------
If numberOfTimes is small and someString is short, the simple concatenation
is as fast (or a bit faster) simply because there's less overhead involved
there than in list manipulations. But as either of those gets bigger, it
soon begins to bite.
Again, think what is happening in either case:
In the first case, every time the loop repeats some string data is copied
and a concatenation is performed. Each time this happens x increases in
length, so more data must be copied around in memory the next time.
In the second case, you gain twice: Firstly, you can take advantage of the
AS list object's mutability to avoid performing lots of list object copying
by simply adding new items to the end of the existing list [i.e. "set y's
end to someString" is faster than "set y to y & someString", so make use of
it]. Secondly, instead of performing many incremental concatenations that
grow slower and slower each time, you simply carry out one big one at the
end.
(I'm sure someone who remembers their high school maths better than I could
better express these cases as formal equations, but you get the idea.)
--
All of which is a good reason to get Jon's Commands or PrecisionTimer and
spend a bit of time experimenting with these algorithms, seeing how well
(or badly) each performs under different circumstances. It's hard to
quantify exactly, but after a while you generally get a feel for these
things.
HTH
has
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.