I have some code that I want to profile to see how much time each step takes, for instance:
myFuncLocal = Function[{f},
Block[{df = {{1, 0, 0}, {0, 2, 0}, {0, 0, 3}}.f, g},
g = f^2 + 3 df + f.df;
LinearSolve[{{1, 0, 0}, {0, 2, 0}, {0, 0, 3}}, g]
]
];
I can do this by turning on Evaluation
-> Debugger
and evaluating:
ftest = RandomReal[{}, 3]
myFuncLocal[ftest] // RuntimeTools`Profile
and this will tell me how long each of the steps took.
But what if I define myFuncPackage
exactly the same way, but inside a package testPackage.m
? Now,
Needs["testPackage`"]
ftest = RandomReal[{}, 3]
myFuncPackage[ftest] // RuntimeTools`Profile
just tells me how long the function as a whole took, not the individual steps inside.
Ideally I want to see as many steps as possible by just applying a function outside of the package, as above. But if I have to put something like If[$Profile,RuntimeToolsProfile,Identity]
at one or a small number of places inside the package that would be ok too.
The actual package I'm working with is more complicated than the example above, and has a more complicated structure, and I would like to see as much of the steps as possible.
Is this possible at all? Nothing seems to work except running everything in the package notebook, which means not using a package.
Added: Ideal would be a solution that can be used together with Carl Woll's answer to this question, which avoids the need to set a debug mode and shows the result in the same notebook instead of opening a new one.