# Profiling a package

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] // RuntimeToolsProfile


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] // RuntimeToolsProfile


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.

• Have you tried using the profiler in Workbench? reference.wolfram.com/workbench/index.jsp?topic=/… I have not used this in a very long time and I do not have time to set it up now and give you details. But I remember that years ago it worked. – Szabolcs Dec 7 '18 at 12:51
• wolfram.com/broadcast/video.php?c=93&v=895 – Szabolcs Dec 7 '18 at 13:59
• @Szabolcs Thanks I'll look into that. When you say you remember that years ago it worked, do you mean that it worked specifically with a package as well? – Jansen Dec 9 '18 at 10:05
• That should be the case because Workbench is specifically for editing packages (not notebooks). – Szabolcs Dec 9 '18 at 10:11
• @Szabolcs Ok, I will report back once I figure it out. Still, it'd be much more convenient to be able to do it directly from a notebook, so if anyone knows how I'm still interested! – Jansen Dec 9 '18 at 11:21

6. Now (without first quitting the kernel) you can use Profile` and it will work with the package