Re: Speed of applets vs. compiled scripts
Re: Speed of applets vs. compiled scripts
- Subject: Re: Speed of applets vs. compiled scripts
- From: Arthur J Knapp <email@hidden>
- Date: Mon, 28 Jan 2002 12:33:20 -0500
>
Date: Mon, 28 Jan 2002 11:18:36 -0500
>
From: "Marc K. Myers" <email@hidden>
>
Subject: Speed of applets vs. compiled scripts
>
I wrote a little script to generate a list of all the prime numbers from
>
2 to 10001.
I also wrote a random number script, but it breaks at the oft-mention
limit of around 4000 items.
>
... I ran it under Script Editor and it took 586 seconds. Just
>
out of curiosity I ran the same script as an applet. It ran for 998
>
seconds! Can anyone tell me why there is such a difference?
Nope. :(
>
... Also, is there a faster algorithm for
>
generating a list of primes?
Oh, you want speed... ;-)
This is the script that I posted to AppleMods. I was just going
to provide a link, but I can't seem to get to AppleMods at the
moment, <
http://www.applemods.com/>.
-- Based on the "Sieve of Eratosthenese" algorithm:
on GetPrimes( max_boundry )
set prime_list to {1}
repeat with i from 2 to max_boundry
set end of prime_list to i
end repeat
repeat with i from 2 to max_boundry
if (item i of prime_list is not missing value) then
repeat with j from (i * i) to max_boundry by i
set item j of prime_list to missing value
end repeat
end if
end repeat
return every integer of prime_list
--
--> breaks at around 4000 some integers
end GetPrimes
One (secret) way to speed up working with large lists is to work
with the "a reference to" operator. The trick to using this operator
inside a handler is to create a script object:
on GetPrimes(max_boundry)
script obj
property obj_list : {1}
end script
set prime_list to a reference to obj's obj_list
repeat with i from 2 to max_boundry
set end of prime_list to i
end repeat
repeat with i from 2 to max_boundry
if (item i of prime_list is not missing value) then
repeat with j from (i * i) to max_boundry by i
set item j of prime_list to missing value
end repeat
end if
end repeat
return every integer of prime_list
end GetPrimes
We can actually "fix" the handler to return any practical number of
primes by replacing:
return every integer of prime_list
with:
return GetIntegers(prime_list)
where GetIntegers() is:
(*
* This is a direct modification of the everyTextItem() handler
* from the everyItemLib script library, available from:
*
* <
http://www.applemods.com/getMod.php?script_ID=50>
*
* Kudos to the mysterious (has) for everyItemLib. I could
* have done better, but why bother? ;-)
*)
property kGetListLimit : 4000
on GetIntegers(lst)
set lim to kGetListLimit
repeat -- technique to ensure that kGetListLimit is not too high
try
set integerCount to count lst each integer
if (integerCount < lim) then
return every integer of lst
end if
set theList to {}
set endLen to integerCount mod lim
set blockCount to integerCount - endLen
repeat with x from 1 to blockCount by lim
set theList to theList & ,
lst's integers x thru (x + lim - 1)
end repeat
if (endLen is not 0) then
set theList to theList & ,
lst's integers -endLen thru -1
end if
return theList --> and break out of repeat
on error number -2706
(*
* lim is too high, use (has)'s "self-correcting"
* technique.
*)
set lim to (lim div 4) * 3 -- and continue repeat loop
end try
end repeat
end GetIntegers
{ Arthur J. Knapp, of <
http://www.STELLARViSIONs.com>
<
mailto:email@hidden>
try
<
http://www.facespan.com/>
on error number -128
end try
}