Optimization can be tricky

Brian Milby brian at milby7.com
Tue Jun 12 01:16:59 EDT 2018


Do you need the entire list sorted randomly or are you just needing to select N random entries from the list (weighted)? How does the speed change if you do a simple numeric sort?

You could use a function on a random number to weight things - would need to work out the right math to get what you want. Something like random(n)^2/n^2 possibly. Depending on how heavily you want to prefer one end, you could use log or ln.

Your initial code is calculating “item 1 of interestArray[uID][T]” once or twice for every line S. I’m guessing that if you evaluate that to a variable once per repeat it would provide a small gain.

Depending on how often the if turns out to be true, some additional variables could possibly reduce the number of duplicate calculations enough to offset the cost of the assignment (what Jerry mentioned).
On Jun 11, 2018, 8:30 PM -0500, Jerry Jensen via use-livecode , wrote:
> At first glance, it looks like you might save some time by grabbing interestArray[uID][T] as soon as you have T, and then use what you grabbed in the 3 later places instead of re-figuring interestArray[uID][T] each time.
>
> As in:
> repeat for each line T in the keys of interestArray[uID]
> put interestArray[uID][T] into AuT —- grab it here
> repeat for each line S in storyArray[T]
> if abs(item 2 of S - item 1 of AuT) < 20 \
> and userSeenArray[uID][item 1 of S] < 4
> then put (101 + userSeenArray[uID][item 1 of S] * 30 + 5 * \
> abs(item 2 of S - item 1 of AuT) - \
> item 2 of AuT),T,S & cr after candidateList
> end repeat
> end repeat
> sort lines of candidateList numeric by random(item 1 of each)
>
> It looks like you could do a similar thing with userSeenArray[uID][item 1 of S] to avoid repeating the same calculation.
> Also with item 1 of AuT, and item 2 of S, with lesser gains but while you’re at it…
> That all will make it easier (or harder) to read, depending on if you can find descriptive variable names for the intermediate values.
>
> It won’t help the sort speed, but saves a lot of array un-hashing.
> I haven’t figured out what the algoraithm is doing, or the idea of the randomness in the sort.
>
> All untested, of course!
> Cheers,
> Jerry
>
> > On Jun 11, 2018, at 5:21 PM, Geoff Canyon via use-livecode <use-livecode at lists.runrev.com> wrote:
> >
> > My first pass at the routine looked roughly like this:
> >
> > repeat for each line T in the keys of interestArray[uID]
> > repeat for each line S in storyArray[T]
> > if abs(item 2 of S - item 1 of interestArray[uID][T]) < 20 \
> > and userSeenArray[uID][item 1 of S] < 4
> > then put (101 + userSeenArray[uID][item 1 of S] * 30 + 5 * \
> > abs(item 2 of S - item 1 of interestArray[uID][T]) - \
> > item 2 of interestArray[uID][T]),T,S & cr after candidateList
> > end repeat
> > end repeat
> > sort lines of candidateList numeric by random(item 1 of each)
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode



More information about the use-livecode mailing list