kaveh1000 wrote: ↑Wed Sep 04, 2024 9:07 am
And a quick question - am I right that arrays are much faster than loops, say, in general?
As Stam pointed out that you will have to loop.
But there are different loops, some faster some slower.
In my experience it all depends on what you are trying to achieve.
If you have a list, like in your example xml, working with the list in a repeat loop:
Code: Select all
repeat for each line aLine in myList
In this form
aLine contains the text of that line and you can act upon it
The same list in the form
Code: Select all
repeat with i = 1 to the number of lines of myList
put line i of myList into tLineText
...
is very slow for longer lists because on each iteration you force LC to fetch
line i of myList to put it into a variable.
On the other hand creating an array from a list
has its overhead too. Creating an array is not free.
Then it also depends on how you access the array.
In above sample you have an array that is numerically indexed by a key
However in arrays the keys are not sorted numerically. You would have to put the keys into a list and sort that list to use the keys from the list to access the array values.
Stam uses a faster approach:
Code: Select all
repeat for each element anElement in tArray
But you have to know whether your particular array is suited for this approach.
I attach a stack that demonstrates different approaches to loop for lists. Please have a look at the code.
Click on a radio button and then try different versions of loops.
It uses the colorNames of LC to create lists of different lengths to compare speed.
Kind regards
Bernd