# Profiling from Mathematica

I've always wished I could do some profiling like you get in Wolfram Workbench, but directly from Mathematica, without using or having Workbench. If it is possible, how can I do it?

-
Related question. –  Leonid Shifrin Jul 1 '12 at 20:10
I love the Please help me! part –  belisarius Jul 1 '12 at 20:23
also related stackoverflow.com/questions/4721171/… –  belisarius Jul 1 '12 at 20:24

You can put your Mathematica session in debug mode by going to Evaluation->Debugger

Then, make some definitions and wrap the profiled code in RuntimeToolsProfile

For example, in debug mode, run

f[x_] := x^2

Table[f[x], {100000}]; // RuntimeToolsProfile


and you get a nice

As @acl mentioned in the comments, clicking in the gray area in the output notebook's lines takes you to the related code

-
Any idea, why test[] := Module[{}, Print["Here"]; Pause[0.1]; Print["There"]; Pause[1]; Return[2]] and test[] // RuntimeToolsProfile does not work, even though your example works correctly? –  Ajasja Jul 2 '12 at 8:37
Also, do you happen to know what RuntimeToolsProfileFunction does? –  Ajasja Jul 2 '12 at 10:15
@Ajasja, sorry about the name ;). I am not sure. It don't think this profiler is without quirks. For starters, I think it doesn't account for Pauses, and without those pauses your code is too fast –  Rojo Jul 2 '12 at 19:55
@Ajasja, I don't konw what that function does. It doesn't seem to be directly used by the profiler but, it seems to receive a matrix in which each row represents a function, the first column is the number of calls, the second the time taken overall, the third the code in question, and the fourth some number that relates either to the order of execution or something it uses to filter what to show. And returns the profiler notebook –  Rojo Jul 2 '12 at 20:07
Unfortunately this solution does not seem to work for more complicated constructs, like funcitons called within Module, etc.. –  Wizard Oct 17 '14 at 11:40