File Exchange

## Counting the Floating Point Operations (FLOPS)

version 1.0.0.0 (576 KB) by Hang Qian

### Hang Qian (view profile)

Scan and parse each line of MATLAB codes, and infer FLOPS based on matrix sizes

Updated 23 Apr 2015

The program counts FLOPS of a MATLAB file, either as a script or function. By scanning and parsing each line of the MATLAB codes, we infer the floating point operations based on matrix sizes. Arithmetic operations, matrix decompositions, elementary functions and common statistics functions are counted. The program supports user-defined new rules, which can override our predetermined rules by a spreadsheet. For details of software usage, refer to the enclosed PDF documentation ‘User Guide for FLOPS’.
Usage:

Step 1: Prepare your MATLAB codes in a script or function, say fileName.m.

Step 2: Save all the variables in a MAT file. For example:
save MATfileName.mat

Step 3: Profile the MATLAB codes.
profile on
(run your MATLAB codes here, say filename(A,B,C,D))
profileStruct = profile('info');

Step 4: Count FLOPS by calling the function
FLOPS(fileName,MATfileName,profileStruct);

The parsing and counting results will be displayed on the screen and sent to output arguments.

Hang Qian

### Hang Qian (view profile)

Hi Mohamed,
Thank you very much for the bug report. The flop count software does not know how MATLAB parses code, and the software only recognizes simple code patterns. Users are encouraged to simplify code so that the software can identify the operators and the corresponding flops. I try my best to revise the software so that it will recognize more complicated code patterns.

mohnait

### mohnait (view profile)

Hi Hang,

Thank you for this useful tool.
I noticed a bug related to Matlab comments.
I tested your tool putting only: "x = magic(3);%+magic(3);" in a script and I got: "FLOPs = 9 * 1 = 9", where it should be zero.
I got the correct "FLOPs = 0 * 1 = 0" After adding a space between the semicolon and the % symbol for comments.

Cheers,
Mohamed

Hang Qian

### Hang Qian (view profile)

Hi Darshan,
Thank you for your interest in the FLOPS counter. If we are aware of the theoretic FLOPS for a function, we could add a rule to the spreadsheet ExtendedRules.xlsx. For example, suppose that A is a m-by-p matrix and we believe SVD requires 2np^2+2n^3 FLOPS, we could add a row to the spreadsheet (with three columns), say

First column: svd
Second column: 2*nrow*ncol^2+2*nrow^3
Third column: some arbitrary text for comments

After running the program, the second output argument “Details” should show how much FLOPS are counted for each line of the code. If the extended rule is recognized by the software, we should see the additional FLOPS shown in “Details”.

Regards,
Hang Qian

Darshan Patil

### Darshan Patil (view profile)

Hi Hang,

Since you mentioned this code does not compute flops for nested functions, I was wondering how one can have a work around in order to compute flops for built in matlab functions like null() or svd() etc.. Thanks for your contribution.

Hang Qian

### Hang Qian (view profile)

Hi Gillani,

Thank you for your interests in the FLOP counting tool.

If the arithmetic operations are based on complex numbers, you may consider overriding the internal rules by editing the spreadsheet ExtendedRules.xlsx. Basically, overload +, -, *,/, ect in the first and second columns of the spreadsheet.

Alternatively, you can modify the codes directly by rewriting the internal rules. From about Line 1130, there is a switch...case... block, in which FLOPS of arithmetic operations + - * / are defined.

GILLANI

### GILLANI (view profile)

Hi Hang Qiang,
Thanks for the nice tool. When I multiply two complex numbers, it shows 1 FLOP, which is actually 6. Any idea that how to fix this? Actually, my application is based on matrices with complex values.

jeyakarthikeyan P V

### jeyakarthikeyan P V (view profile)

thank you for valuable and important contribution. It is very much useful to my research.

Ahvand

### Ahvand (view profile)

Thanks Hang for your useful codes. I will cite your codes if I succeed to publish a paper.

Hang Qian

### Hang Qian (view profile)

Hi Alireza,

Thank you for your interest in my codes. I developed that tool when I was working on a research paper. I tried my best to make the FLOPS counting as accurate as possible. Comments and bug reports are welcome and sincerely appreciated.

As for the citation, I think the file exchange ID and the version number is unique to each MATLAB central submission, and other researchers can retrieve the original file to reproduce the results. So I would suggest citation like

Qian, Hang (2015). Counting the Floating Point Operations (FLOPS), MATLAB Central File Exchange, No. 50608, Ver. 1.0, Retrieved June 30, 2015.

Hi Hang,

Thanks for the file. It's very useful in my research. How can I cite your work in my papers?

Thanks,
Alireza

##### MATLAB Release Compatibility
Created with R2014a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux

### Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.