# flops in Matlab

`Somebody asked how one may count the number of floating point operations in a MATLAB program.Prior to version 6, one used to be able to do this with the command `flops`, but this command is no longer available with the newer versions of MATLAB.`flops` is a relic from the LINPACK days of MATLAB (LINPACK has since been replaced by LAPACK). With the use of LAPACK in MATLAB, it will be more approrpiate to use `tic` and`toc` to count elapsed CPU time instead (cf. `tic`,`toc`).If you're interested to know why `flops` is obsolete, you may wish to read the exchanges in NA digest regarding `flops`.Nevertheless, if you feel that you really do need a command to count floating point operations in MATLAB, what you can do is to install Tom Minka's Lightspeed MATLAB toolbox and use the flops counting operations therein.@cise.ufl.edu>`
`@cise.ufl.edu>`
`To count flops, we need to first know what they are.  What is a flop?LAPACK is not the only place where the question "what is a flop?" isrelevant. Sparse matrix codes are another.  Multifrontal and supernodalfactorization algorithms store L and U (and intermediate submatrices, forthe multifrontal method) as a set of dense submatrices.  It's moreefficient that way, since the dense BLAS can be used within the densesubmatrices.  It is often better explicitly store some of the numericalzeros, so that one ends up with fewer frontal matrices or supernodes.So what happens when I compute zero times zero plus zero?  Is that a flop(or two flops)?  I computed it, so one could argue that it counts.  But itwas useless, so one could argue that it shouldn't count.  Computing itallowed me to use more BLAS-3, so I get a faster algorithm that happens todo some useless flops.  How do I compare the "mflop rate" of twoalgorithms that make different decisions on what flops to perform andwhich of those to include in the "flop count"?A somewhat better measure would be to compare the two algorithms based anexternal count.  For example, the "true" flop counts for sparse LUfactorization can be computed in Matlab from the pattern of L and U as:        [L,U,P] = lu (A) ;        Lnz = full (sum (spones (L))) - 1 ;    % off diagonal nz in cols of L        Unz = full (sum (spones (U')))' - 1 ;  % off diagonal nz in rows of U        flops = 2*Lnz*Unz + sum (Lnz) ;The same can be done on the LU factors found by any other factorizationcode. This does count a few spurious flops, namely the computation a_ij +l_ik*u_kj is always counted as two flops, even if a_ij is initially zero.However, even with this "better" measure, the algorithm that does moreflops can be much faster.  You're better off picking the algorithm withthe smallest memory space requirements (which is not always the smallestnnz (L+U)) and/or fastest run time.So my vote is to either leave out the the flop count, or at most return areasonable agreed-upon estimate (like the "true flop count" for LU, above)that is somewhat independent of algorithmic details.  Matrix multiply, forexample, should report 2*n^3, as Cleve states in his Winter 2000newsletter, even though "better" methods with fewer flops (Strassen'smethod) are available.Tim DavisUniversity of Floridadavis@cise.ufl.edu@cise.ufl.edu>`