Algorithm for Direction Change upon Collision

RunRevPlanet feed at smpcsupport.com
Sat Nov 9 23:49:49 CET 2013


Hi Alex,

If you re-wrote the code using arrays and found no difference then you are right
about the array vs list issue for this example.

The reason I made the point, is that in separate benchmarking I have found that,
if a list has less than a few hundred items and each list item is only a few
characters long then accessing a list is often faster than an array.

Recently I was working on some game code with nested loops that just wasn't fast
enough. I found that the processing for each frame of animation was taking
between 40 to 70 milliseconds. That may not sound like much, but when the goal
was 30 fps it was a major problem.

I love using LiveCode arrays because they *are* super fast and very convenient,
but out of curiosity I changed most of the data structures in the inner loops to
lists from arrays. I was very surprised that the processing mentioned above was
reduced to 30 to 50 milliseconds which in the context was a huge improvement.

I agree with you that:

> 2. Don't underestimate the speed of text lists in LiveCode.
> When the number of items is measured in the hundreds, avoid
> using arrays in any code that is repeated in a loop.

is too strongly worded in the context of the code of the particle example, but
as a general principal I would say:

2. Don't underestimate the speed of text lists in LiveCode. When the number of
items is measured in the hundreds and the items are only a few characters in
length, try using a list instead of an array if your code is not fast enough and
the data is mainly being read inside a loop with thousands of iterations.

Thank you for noticing and letting me know of the bug in this line at the top of
the loop:

  if item 1 of collisionList then

You are exactly right. I will fix this and upload a corrected demo.

Cheers,
--
Scott McDonald
"Components, Controls, Tools and Resources for LiveCode"
www.runrevplanet.com



More information about the use-livecode mailing list