elements VS number of lines of the keys

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
jameshale
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 312
Joined: Thu Sep 04, 2008 6:23 am
Location: Melbourne Australia

elements VS number of lines of the keys

Post by jameshale » Fri Apr 29, 2016 7:01 am

TESTED IN --- LC 8 RC1
I was interested in one of the pull requests that mentioned a performance increase.

The changes included the use of elements of an array and I looked up the API as read... (I have reformatted it)
-----
The structure <

Code: Select all

repeat for each element myElement in myArray 
... 
end repeat

is equivalent to

Code: Select all

repeat with x = 1 to the number of lines in the keys of myArray put myArray[line x of the keys of myArray] into myElement
 ... 
end repeat

However, the first form, using the element keyword, is much faster.
-----
I was interested if there was an appreciable gain as I do a lot of array processing in my app.
I decided to time the two forms to see if it was worth my while to change my existing handlers.

The first thing I discovered was that, with respect to repeat loops, elements only works with one dimensional arrays.

I also discovered that you mileage will vary as the assertion of "for each element" being much faster is not quite true.

I tested repeat loops with the following forms:
T1

Code: Select all

repeat with x = 1 to the number of lines in the keys of tarray 
T2

Code: Select all

put the number of lines in the keys of tarray into tx
   repeat with x = 1 to tx
T3

Code: Select all

repeat for each element x in tarray
Here are a sample of my results (TX average time (min time, max time))

Testing 1000 repetitions upon and array of 1000 elements.
t1 : 0.002021 (0.001,0.007)
t2 : 0.002018 (0.001,0.008)
t3 : 0.002452 (0.002,0.014)

As you can see the "for each element" form is not all it is cracked up to be.

Wondering if the comment was PRE LC 8 I decided to run the test in LC 6.7

Testing 1000 repetitions upon and array of 1000 elements.
t1 : 0.000812 (0,0.002)
t2 : 0.000803 (0,0.002)
t3 : 0.000614 (0,0.001)

Array operations are blistering fast in LC 6.7 AND the for each element for is faster.

Moral of the story: If the API says something is faster, try it out. What may have been the case pre 7 may no longer hold.
Attachments
elements test6.livecode.zip
test stack LC 5.5 format
(1.87 KiB) Downloaded 65 times

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 87
Joined: Thu Apr 13, 2006 6:25 pm

Re: elements VS number of lines of the keys

Post by rkriesel » Fri Apr 29, 2016 9:39 am

Hi, James. You could try another form:

Code: Select all

repeat for each key tKey in myArray
If you do, please post the timings.
-- Dick

livecodeali
Livecode Staff Member
Livecode Staff Member
Posts: 171
Joined: Thu Apr 18, 2013 2:48 pm

Re: elements VS number of lines of the keys

Post by livecodeali » Fri Apr 29, 2016 10:01 am

Hi James,

Thanks for doing this! Your test stack is slightly and crucially erroneous though-

Code: Select all

   
repeat for each element x in tarray
      put  tarray[x] *2 into tarray[x]
end repeat
x here is the actual array element, not the key. So the extra processing time you are seeing is the engine attempting to index the array with an array element.

Changing it to

Code: Select all

   
repeat for each key x in tarray
      put  tarray[x] *2 into tarray[x]
end repeat
Gives the following result:

Code: Select all

Testing 1000 repetitions upon and array of 1000 elements.
t1 : 0.003588 (0.003,0.01)
t2 : 0.003615 (0.003,0.004)
t3 : 0.001234 (0.001,0.002)
And using for each element correctly (I had to change all of the tests slightly to make this fair)

Code: Select all

   
repeat for each element x in tarray
      get x * 2
end repeat
gives the following result:

Code: Select all

Testing 1000 repetitions upon and array of 1000 elements.
t1 : 0.002088 (0.001,0.011)
t2 : 0.002028 (0.001,0.006)
t3 : 0.000758 (0,0.001)

jameshale
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 312
Joined: Thu Sep 04, 2008 6:23 am
Location: Melbourne Australia

Re: elements VS number of lines of the keys

Post by jameshale » Fri Apr 29, 2016 10:22 am

<blush>
Your test stack is slightly and crucially erroneous though-
What a kind way of saying you totally @#@#@ up

Yes, I did not understand the definition!

Apologies.

Thanks Ali,

for the correction, clarification and gentleness.

Post Reply

Return to “Talking LiveCode”