gcanyon at gmail.com
Fri Jan 20 10:55:26 EST 2023
On Fri, Jan 20, 2023 at 5:06 AM Alex Tweedly via use-livecode <
use-livecode at lists.runrev.com> wrote:
> Fascinating. Thank you so much for that Geoff.
> I've been afraid to play with ChatGPT so far - too worried abut getting
> sucked in and spending way too much time ....
> I did take a look at your third example (since I can never resist a
> performance challenge :-)
> There are a number of minor tweaks that could be made to improve
> 1. set initial value to infinity rather than calculating a distance
> between the first two points.
I thought of this -- especially since ChatGPT's first (python-esque)
example uses "inf" -- but what would you use as "infinity" in LiveCode? In
practice if I were coding and comfortable with it I'd probably just throw
in 999999999 and be done, but in the abstract, there are bigger numbers
allowed in LC, and I don't care to find the absolute largest -- plus it
would be some weird long string -- I think, I don't know off the top of my
head what the largest 64-bit value is.
> 2. "number of elements in pPoints" is unvarying within any one call - so
> extract it to a variable at the start
Good point. I did benchmark "number of elements" -- ChatGPT's code;
against "item 2 of the extents of" -- what I would have done; and number of
elements is *much* faster.
> 3. use the square of the distance rather than the actual distance - save
> N**2 calls to sqrt
> 4. use "DX * DX" rather than "DX ^ 2" (about 25% faster)
Really? Interesting, I'll have to check that.
> 5. calculate distance in-line rather than call a function
Yep, this is purely an artifact of how ChatGPT wrote it. But it would be
interesting to give it both chunks of code and say "merge this" and see if
it can pull that off. Thanks for the idea!
> but those all add up to maybe 10% performance improvement (or less - I
> didn't test it). That's useful - but not enough.
> For a modest number of points (2000 random points), this takes approx
> 16.5 seconds !!
> We need a better algorithm. If we use a "linear scan", we can change it
> from essentially Order(N**2) to approx Order(N).
> - sort the points by X coordinate
> - while scanning the inner loop, as soon as the difference in Xcoord
> from the 'outer' point exceeds the minDist so far, you can reject not
> just this point, but all subsequent points, and hence exit the inner
> loop immediately.
> This brings the time down from 16500 millisecs to 25 millisecs.
> BUT - I have no clue how I'd go about describing this to ChatGPT :-)
> NB I changed the input parameter to be the list of points rather than
> the array.
Fair point -- the array was entirely ChatGPT's choice, and I didn't
challenge it. I'll ask it to change and see what happens.
> function closestPointsSQ pLines
> sort pLines by item 1 of each
> put pLines into pPoints
> split pPoints by CR
> put infinity into minDist
> put the number of elements in pPoints into N
> repeat with i = 1 to N-1
> repeat with j = i + 1 to N
> put item 1 of pPoints[j] - item 1 of pPoints[i] into t1
> if t1 * t1 > minDist then exit repeat
> put item 2 of pPoints[j] - item 2 of pPoints[i] into t2
> put t1 * t1 + t2 * t2 into dist
> if dist < minDist then
> put dist into minDist
> put pPoints[i] & " " & pPoints[j] into closestPoints
> else if dist = minDist then
> put return & pPoints[i] & " " & pPoints[j] after closestPoints
> end if
> end repeat
> end repeat
> return closestPoints
> end closestPointsSQ
> -- Alex.
> On 20/01/2023 06:02, Geoff Canyon via use-livecode wrote:
> > I tested three use cases, with variations, using ChatGPT for (live)code
> > generation. There was a lot of back and forth. In the end, I solved all
> > problems I set, but in some cases I had to hold ChatGPT's hand pretty
> > tightly.
> > That said, I learned some things as well -- about LiveCode. ChatGPT's
> > for Fizz Buzz was faster than mine.
> > My code was faster for reversing lines. But ChatGPT, when asked to "make
> > the code faster" gave several suggestions, some of which were wrong or
> > impossible, but one of them was my method, and when I said "write that
> > option" it did, with only a few corrections needed.
> > And one of the ideas, while wrong, caused me to think of a different way
> > solve the problem, and that way ended up being faster than my original
> > solution by over 3x on reversing 10k lines. Getting ChatGPT to write this
> > new method was *hard*.
> > In any case, I wrote it all down in a google doc
> > <
> > If you're curious, have a read. That URL is open for comments/edit
> > suggestions. If you have any I'd love to hear them.
> > Thanks!
> > Geoff
> > _______________________________________________
> > 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
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
More information about the use-livecode