Re: Sum of list?
Re: Sum of list?
- Subject: Re: Sum of list?
- From: Nigel Garvey <email@hidden>
- Date: Wed, 30 Jun 2004 23:15:43 +0100
Martin Orpen wrote on Wed, 30 Jun 2004 12:14:46 +0100:
>
on 29/6/04 11:35 pm, Nigel Garvey at email@hidden
>
wrote:
>
[snip]
>
> repeat with i from lenMod10 + 1 to listLength by 10
>
> set sum to sum + (item i of o's l) + (item (i + 1) of o's l) + ,
>
> (item (i + 2) of o's l) + item (i + 3) of o's l + ,
>
> (item (i + 4) of o's l) + item (i + 5) of o's l + ,
>
> (item (i + 6) of o's l) + item (i + 7) of o's l + ,
>
> (item (i + 8) of o's l) + item (i + 9) of o's l
>
> end repeat
[snip]
>
However, maybe I'm missing something, but can't you dispense with all that
>
typing and just have another nested repeat?
>
>
repeat with i from lenMod10 + 1 to listLength by 10
>
repeat with x from 0 to 9
>
set sum to sum + (item (i + x) of o's l)
>
end repeat
>
end repeat
>
>
Both versions seem to take identical amounts of time on my laptop (24
>
seconds for numbers 1 thru 100000).
I'm on my old 4400 at the moment, where the difference is quite marked,
even with just 50000 integers. (Longhand version:3.568 seconds, nested
loops:5.571 seconds.) The point of the longhand method is to reduce the
amount of time the script spends executing loop instructions. On my
machines, at least, it takes less time to do one iteration with 19
additions than to do ten iterations with 1 addition each. The difference
isn't very much on my PowerBook, but it does make the difference between
being faster or slower than 'statlist'. ;-)
With your nested repeats, the inner loop does the same number of
iterations overall as a single straight repeat from 1 to listLength,
except that it's now doing two additions per iteration. Also, every ten
of its iterations, it has to be set up again and one iteration of the
outer loop has to be marked off. It's thus actually slower than a
straight repeat.
Emmanuel wrote on Wed, 30 Jun 2004 11:29:57 +0200:
>
This being said, on my old G3/900MHz the vanilla 'repeat' is just a little
>
*slower* than statlist (it requires quite precisely 50% more time: 7
>
microsecond vs 10.5 per item).
The vanilla was only a few hundredths of a second faster over a whole
list on my G3/400 PowerBook (testing in SE 2.0 on 10.2.8). It's not
really a fair comparison, because the vanilla concentrates on the sum and
the average, whereas 'statlist' returns three other statistics as well.
The reason I mentioned it was because the original poster thought that
using something other than a repeat was the only way to speed up his
script.
Martin Orpen wrote on :
>
If you're working on consecutive numbers you can always use a "Gaussian"
>
method:
>
>
set n to (text returned of (display dialog "Calculate the sum of the
>
first n integers, where n is:" default answer "")) as number
>
set x to ((n * (n + 1)) / 2) as inches
>
display dialog "The sum of the first " & n & " integers is: " & (x as
>
string)
>
>
I doubt that you'd be able to measure the microseconds per item on that one
>
:-)
:-)
It wouldn't even be worth optimising ((n * (n + 1)) / 2) to ((n * n + n)
div 2). ;-)
NG
_______________________________________________
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.