12
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Szabolcs Dec 7 '18 at 12:51
  • $\begingroup$ wolfram.com/broadcast/video.php?c=93&v=895 $\endgroup$ – Szabolcs Dec 7 '18 at 13:59
  • $\begingroup$ @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? $\endgroup$ – Jansen Dec 9 '18 at 10:05
  • $\begingroup$ That should be the case because Workbench is specifically for editing packages (not notebooks). $\endgroup$ – Szabolcs Dec 9 '18 at 10:11
  • $\begingroup$ @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! $\endgroup$ – Jansen Dec 9 '18 at 11:21
7
$\begingroup$

This is not an ideal answer, but it is possible with Workbench, this is how (thanks to Szabolcs):

  1. Install Workbench as an Eclipse plugin following this http://support.wolfram.com/kb/27221 (choose the IDE for java developers)
  2. In Eclipse go to File->New Project->Wolfram->Application Project
  3. Choose a name and leave everything as default, this will create name.m and name.nb
  4. Copy your package into name.m and the notebook that uses it into name.nb (the code that loads the package is not needed)
  5. Right click name.nb and choose debug as -> Wolfram
  6. Now (without first quitting the kernel) you can use Profile and it will work with the package

I will leave the question open for an answer that doesn't require Workbench.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.