# Optimization can be tricky

hh hh at hyperhh.de
Tue Jun 12 07:44:14 EDT 2018

```Sorry, I forgot to mention Jerry who already proposed to pull out computations
from the inner loop and also to use variables in the inner loop. My experience
says to avoid getting items as often as possible. And to use the random() in the
inner repeat loop instead of in the final sort may be worth a trial with a large
data set.

I wrote:

> You could try the following.
>
> repeat for each key T in interestArray[uID]
>   put item 1 of interestArray[uID][T] into i1
>   put item 2 of interestArray[uID][T] into i2
>   repeat for each line S in storyArray[T]
>     put userSeenArray[uID][item 1 of S] into s1
>     put abs(item 2 of S - i1) into s2
>     if s2 < 20 and s1 < 4 then
>       put random(101 + s1 * 30 + 5 *  s2 - i2),T,S & cr after candidateList
>     end if
>   end repeat
> end repeat
> sort candidateList numeric by item 1 of each
>
>
> Ali wrote:
> repeat for each key T in interestArray[uID]
>
> Geoff wrote:
> 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)

```