Re: Removing (or picking) items from list 1 via list 2?
Re: Removing (or picking) items from list 1 via list 2?
- Subject: Re: Removing (or picking) items from list 1 via list 2?
- From: "charles" <email@hidden>
- Date: Tue, 23 Apr 2002 22:37:23 +0100
At 1:01 pm -0700 23/4/02, Michael wrote:
>
(re trying to pick randomly from list1 but not using stuff from list2
>
Here are your biggest speed issues:
>
>
1. Using a reference (e.g. "my list1")
do you mean "a reference to .."?
>
will be faster when accessing
>
lists in certain cases. It doesn't seem to matter with "is in", but
>
*does* matter with "count". So, The other possibility to get rid of
>
count is to do it before the repeat and place it in a variable
>
Sure, I do that. (Curse Eudora for its weird quoting behaviour.)
>
2. Random number. This is an osax call and therefore quite slow within
>
repeats. There are two possibilities. You can use the "some item"
>
feature, to get a random item from list1, or you can download my
>
pseudoRand applemod, which will generate random integers from 1 to n
>
about 40 times as fast as the osax call.
Will it work in OSX? (I'll go have a look.)... hmm, OK. There'll certainly
be lots of it.
>
If your list1 will stay
>
shorter than 500 items, then some item should be roughly as fast. it's
>
order n on the length of list, however, and pseudoRand is not, so if
>
list1 could be very long (more than 2-3,000 items), you should probably
>
use pseudoRand. BTW, if you use "some item", then using "some item of
>
my list1" is much faster than "some item of list1"
Same as above - do you mean "a reference to"?
>
>
> But I wonder if the fastest way eventually might be
>
> Is the best way to create list3, by eg
>
> --
>
> set list3 to {}
>
> set list3ref to a reference to list3
>
> repeat with anitem in list1
>
> if anitem is not in list2 then set end of list3ref to anitem
>
> end repeat
>
> set randomnum to randomnumber from 1 to count items in list3
>
> set mywanteditem to item randomnum of list3
>
> --
>
>
Whether this will be faster (note, the same optimizations I talked about
>
above will apply to this as well) is going to depend on your data set.
>
How likely is a random item from list1 going to be in list2? If almost
>
always, then this should work better. If less than about 90% of the
>
time, then your first handler will probably be faster on average. the
>
exact percentage would need to be sussed out by trial and error.
The problem is, I really don't know! It could be a lot, or it could be that
list2 is empty. Imagine it's for iTunes, and list1 is a list of songs; and
list2 is a list of songs that you've already looked at. Who knows whether
it'll be big or small? I'm making guesses based on my `own stuff, which is
about 20 gigs of music (4,000 tracks). There are all sorts of possibilities
for the best approach, aren't there?
>
>
think about it though -- In the first, you repeat through list1 only
>
until you find a hit. In the second, you repeat through every single
>
item. The second is only going to be faster if there are very few
>
possible hits relative to the size of the list. (Well, if you use
>
random number, it's a wider gap, because you're calling it less often in
>
the second algorithm, but some item or pseudoRand cuts that time down to
>
something like 2-3 regular list accesses instead of 50+).
Good point. It does sound like the first method is better, rather than
creating a "clean" list.
>
>
It might be good to know the context. It's possible there's some major
>
optimization to be had there as well.
My guess is that it's actually a two-stage thing - there's quite an easy
process at first (you have a large list1, and not much in list2) but then
it becomes increasingly hard to find things in list1 that aren't already in
list2. I've gone for two separate methods in the same program, depending
how far through the choosing process one is.
best
Charles
--
http://www.ukclimbing.com : 1,000+ British crags, 350+ British climbing walls
- searchable by distance rock type, etc, with 5-day weather forecasts for
every one - plus maps, articles, news, and the New Routes database. There's
even a cool shop attached...
_______________________________________________
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.