# Counting the number of operations performed during a calculation

I need to know how can I count the number of operations performed during a calculation of a CompoundExpression.

In some of these expressions there are Dot products between matrices and I want to count how many additions a multiplications are performed in each of these calculations.

In other words, I need a functions that does the same as the function cost of Maple (http://www.maplesoft.com/support/help/Maple/view.aspx?path=codegen/cost)

Does anyone know how to do it?

• What kind of expressions did you have in mind? Polynomials and the like shouldn't be hard, but you're probably not going to be able to do it reliably for something way more complicated... – rm -rf Aug 6 '13 at 3:02
• For instance, I want to know how many additions and multiplications Mathematica performs during the evaluation of an expression like LinearSolve[Dot[{{1, 2}, {3, 4}}, {{5, 6}, {3, 4}}], {5, 6}] – Renato Orsino Aug 6 '13 at 3:08
• I'm giving this a vote because I've never seen a question like it, but outside of some external low-level debugging tool I have no idea how one would snoop on that kind of thing. Many numeric operations are passed to libraries such as the Intel MKL and are therefore outside Mathematica's direct control anyway. – Mr.Wizard Aug 6 '13 at 6:05
• @Nasser: it stopped just now..it was there about half an hour ago.It says undergoing maintenance. – Rorschach Aug 6 '13 at 19:41
• This example in the docs claims to "count additions and multiplications of machine numbers needed for a numerical computation". – Michael E2 Aug 6 '13 at 19:47

This is NOT AT ALL answer to this question, but I am just writing my opinion here. Link shown for Maple, counts only number of atomic operations and doesn't inform anything about kernel level instructions which I believe is not more significant than Timing.I tried the following and it gives the same kind of output. All that is needed is to put it together in a function. For example,

t = {et = Plus[a x^2 + (b/s)  x + c, x], Times[a, b]} // FullForm


than counting operations as,

Count[t, _Plus, Infinity] (*7)
Count[t, _Times, Infinity] (*9*)
Count[t, _Power, Infinity] (*5*)
Count[t, Power[a_, b_] /; b < 1, Infinity] (*1*)


In this way operations can be enlisted and total number of instructions can be fetched. I hope I have understood the question correctly and not missing on any subtlety.

Edit: In case of Numeric calculation use HoldForm, as under,

t = HoldForm[123 675 + 234/124 + 34/67 + 23^34] // FullForm
Count[t, _Power, Infinity](*3*)
Count[t, Power[a_, b_] /; b < 1, Infinity](*2*)
Count[t, _Times, Infinity](*3*)

• Actually I have been trying to build some functions based on the answers to this question, which is a generalization of the aproach you are proposing. However, I need something that works with numeric calculations. I am not sure whether or not it is possible. Anyway, thank you for the attention. Also, thank you for clarifying how the cost function works at Maple. – Renato Orsino Aug 6 '13 at 19:18
• actually thanks for link, it should have been listed in comments above,its pretty informative. – Rorschach Aug 6 '13 at 19:24