# Speed of inline vs loop

A neat loop can be very elegant and of course, if you have to change an equation, you only change it inside the loop.

If you have a very short number of elements - like 3 - is it faster to use indexing and array statements to deal with those three cases or faster to just define three things and statements that repeat, except for the variable, three times

For example - this is going to be way simple - if I have three names and I want to uppercase them, I could do:
name1 = upper(name1)
name2 = upper(name2)
name3 =upper(name3)

or set AllNames = name1 + “,” + name2 + “,” + name3
then:
x = 0
loop
x = x + 1
array(AllNames, 1, “,”) = upper(array(AllNames,1,“,”))
until x = arraysize(AllNames, “,”)

In the “real world” it looks more like:
yes - I know I could put that calculation in the if statement instead of using the dif variable
I could even put it in the rep( function if it is happy with a zero count

the point is, I can repeat the code below three times or build a 2-dimensional array with a CR() delimiter between what is now locate1, locate2, locate3

dif = maxfound - arraysize(locate1,“,”)
If dif ≠ 0
locate1 = locate1 + rep(“,”,dif)
endif

but the overhead of that construction may “cost” more than just repeating the code X (where X is your tolerance for repeat code statements) times?

This is a syntax error. You can’t have a function on the left side of an assignment statement.

The statement

``````AllNames = upper(AllNames)
``````

would do the entire array in one fell swoop.

Rather than use the loop statement, and an index variable you keep incrementing with `x = x+1` to step through an array, it’s almost always quicker and easier to use ArrayFilter, or LoopArray.

And, when it’s possible to use `arrayfilter(` (applying the same formula, however complex, to each element of the array) it’s invariably much quicker than any loop, even `looparray`.

Thank you Peter and Dave.

Yeah - syntax error in the example - in addition to the left-hand side issue the index was not in the array reference. The example was more the idea of a short set of statements repeated with a short number of interactions.

With today’s CPU speeds, barely a blink of an eye. Though these speed concerns are trivial in this project, the techniques learned will be valuable later on.

Version 2.0 will incorporate those finesses.
First, make it work.
Then make it pretty.
Then make it faster.

The last two might be swapped depending on the situation.

Indeed. I recently rewrote a complex procedure from a year or two ago, processing a large database, originally written before I fully appreciated the power of `arrayfilter(`. The original took between 2 and 3 minutes to run; after rewriting it to replace (most of) the `looparray`s with `arrayfilter(`s, I got it down to 10 seconds.

,

If you want to get technical about it, arrayfilter is also a loop, but the loop is written in Objective-C code so it is much faster than a loop written in Panorama code (this is because Objective-C code is compiled to machine language, while Panorama code is interpreted).

There’s a name for this - it’s called “unrolling” the loop. In Panorama, the overhead of loops is pretty low, so I think it would be extremely unusual to encounter a situation where unrolling the loop will have a performance advantage. (In fact, that would be unusual in almost any programming language, except in very special situations.)

I think this is a super important point. Sometimes backing up and taking a fresh look at your problem can make all the difference in the world. Or to put it another way, don’t fall in love with a solution - it might be solving a problem you don’t even have!

In particular, a bit of lateral thinking to consider how Panorama’s particular strengths might help. The ability to consider the contents of a text variable or field as simultaneously a single string, a one-dimensional string array and a multi-dimensional array, and apply functions, structures or operators appropriate to each, has blown my mind and led me to far more efficient (and ‘pretty’) solutions to text processing than would have been open to me in any of the other high- or low-level languages I’ve tried, limited (for instance) more rigidly to variables, arrays and loops.

There is always a price to be paid for flexibility and it’s true that extracting 96,845 random elements from a Panorama text array would never be remotely as quick as from a more conventionally indexed array such as a Panorama data array. But that just means more lateral thinking.