Search code examples
performancematlabloopstime-complexityvariable-length

Is it efficient to use length() in loops?


I have a lot of cases when I iterate over a vector in MATLAB,

so I do this :

len = length(vector)
for i=1:len
 do_something();
end

But this is just an intuition that says "prevent calling the length() function every time". Is it true? Or is it the same thing as this, in terms of time requirement :

for i=1:length(vector)
 do_something();
end

Thanks for any help!


Solution

  • If you're concerned about performance or speed - don't worry about it, really. It will not make any appreciable difference. @David gives some timings in his answer that back this up.

    In terms of aesthetics, for what it's worth I would normally write

    for i = 1:numel(v)
        doSomething(v(i))
    end
    

    I tend to prefer numel to length, as length gives you the length of longest array dimension, rather than the length of the first dimension. If you're always sure it's a vector they're the same, but I try to avoid that assumption.

    Of course, if I needed access to numel(v) separately, then it would be worth extracting it to an intermediate variable vNum and writing for i = 1:vNum.

    If you're interested in the semantics of the for loop indices, it's more complex than you think. When you write for i = 1:N, MATLAB does not always simply create the vector 1:N and then iterate through it. For example, you can write

    for i = 1:realmax
        i
    end
    

    or

    for i = 1:Inf
        i
    end
    

    or even

    for i = Inf:-1:1
        i
    end
    

    and it will work (press Ctrl-C to escape). MATLAB doesn't create the loop indices as a vector in these case, and it couldn't, as it would be too large.

    The parser has a more complex logic than that, with knowledge of several edge cases, and optimizations. Sometimes it will create the loop indices ahead of the loop, sometimes on the fly. I guarantee you will not be able to second-guess all the internals, unless you have access to the MATLAB source code.

    Note also that the loop indices do not have to be a vector; if you supply an array, MATLAB loops through the columns. For example,

    for i = magic(3)
        i
    end
    

    displays the columns of the array in turn. Similarly you can supply a cell array and it will iterate through the cells (NB the indices are the cells, not the elements within the cells).

    For this reason, I would sometimes write

    for vElement = v
        doSomething(vElement)
    end
    

    rather than the first pattern above using numel.

    And of course, depending on the application, it may well be better to vectorize doSomething and call doSomething(v).

    One last thing - we're talking so far about integer indices really, but remember that the colon operator can have any increment, such as pi:sqrt(2):10; and remember that the colon operator is not the same as linspace, and will not give you the same answers.