Optimization can be tricky
Jerry Jensen
jerry at jhjensen.com
Mon Jun 11 21:30:26 EDT 2018
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)
More information about the use-livecode
mailing list