9

Which one is better, using all the *fun functions (arrayfun, cellfun, structfun and spfun) or simply using for loop?

What method gives better performance and which methods should be considered better practice in terms of readability of the code?

Shai
  • 111,146
  • 38
  • 238
  • 371
  • 4
    Related question: [arrayfun can be significantly slower than an explicit loop in matlab. Why?](http://stackoverflow.com/questions/12522888/arrayfun-can-be-significantly-slower-than-an-explicit-loop-in-matlab-why) – Eitan T Apr 22 '13 at 09:15
  • 2
    also related [answer](http://stackoverflow.com/a/15181321/1714410) – Shai Apr 22 '13 at 09:17
  • I'd say that it depends quite a lot on which MATLAB release you are uging. In my case, MATLAB R2012a, I'm making use of almost only `for` loops – fpe Apr 22 '13 at 09:25
  • 1
    I'd say that the relative performance of various ways of computing functions over arrays changes as Matlab is developed and that it behoves the professionals among us to (re-)test our thinking about which approach is faster with each new release. As for readability of code, that's very subjective and I fear to tread there. – High Performance Mark Apr 22 '13 at 09:55
  • for-loops are currently the most optimized constructs by the JIT acceleration. The hope is that arrayfun/cellfun/.. will be automatically parallelized sometime in the future – Amro Apr 22 '13 at 09:59
  • 2
    for instance, MATLAB currently has [arrayfun](http://www.mathworks.com/help/distcomp/arrayfun.html) that runs on the GPU for `gpuArray`. Octave has a [parallel arrayfun](http://octave.sourceforge.net/general/function/pararrayfun.html) using multiple processes on the CPU (sort of the equivalent of `parfor`) – Amro Apr 22 '13 at 10:06
  • 1
    another related question with comparisons: [What is the fastest way to perform arithmetic operations on each element of a cell array?](http://stackoverflow.com/questions/15851718/what-is-the-fastest-way-to-perform-arithmetic-operations-on-each-element-of-a-ce) – Amro Apr 22 '13 at 11:13

1 Answers1

17

It really depends on what you call 'performance' :)

If you mean minimum execution time, well, sometimes *fun are faster (for example, cellfun('isempty', ...); (yes, string argument!) for sure beats the loop version). Sometimes a loop is faster. If you're on a Matlab version < 2006, go for the *fun functions by default. If you're on anything more recent, go for the loops by default. You'll still always have to profile to find out which one's faster.

As noted by Amro, if you have a GPU capable of doing FP arithmetic, and a recent version of Matlab that supports GpGPU, then a call to arrayfun for gpuArray inputs will be massively-parallelized. However, no general statements can be made regardnig execution time; for smaller arrays, or absolutely humungous ones, the overhead of copying everything over to the GPU might undo any benefit of parallelizing the computations, so...profiling is really the only way to know for sure.

If you mean minimum coding time, then I'd say it's usually faster to code in terms of *fun as long as the operations are simple. For anything complex it's usually better to go for the loop.

If you mean optimum readability and thus minimum time required for maintenance and implementation of changes in a professional context, for sure, go for the loop.

At this point in time, there's not really a clear-cut simple answer to your question :)

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96