Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
Related question. – Leonid Shifrin Jul 1 '12 at 20:10
10  
I love the Please help me! part – Dr. belisarius Jul 1 '12 at 20:23
    
also related stackoverflow.com/questions/4721171/… – Dr. belisarius Jul 1 '12 at 20:24
3  
A shame the Please help me! has been removed from the original post. – Wizard Jul 9 '15 at 11:14

Not knowing about the built in functions for this, I compiled these two functions. Hope they help someone.

(* Test Performance of expression evaluation *)
benchmark[locals_,expr_,opts:OptionsPattern[]]:=Module[
{timeLst,funcs,stats,totalTime,times=OptionValue[times],run},
progressBar[Dynamic[run],times];
totalTime=AbsoluteTiming[
    timeLst=Table[
        Module[locals,
            ReleaseHold@AbsoluteTiming[expr]][[1]],
        {run,times}]][[1]];
If[OptionValue[printStats],
    styledPrint[{"Stats for ",times," runs of '", HoldForm[expr] ,"':"},Bold,20];
    statsSummary[timeLst];
    (styledPrint[{"Total Time: ", NumberForm[totalTime,3]},16])];
Return[{stats,timeLst}]
];

SetAttributes[benchmark,HoldAll];
Options[benchmark]={printStats->True,times->9};
benchmark::usage="benchmark[locals_,expr_,OptionsPattern[]] takes a list of localized var=val assignments (which are evaluated anew each run) (lexical, via Module). The expression is evaluated the given number of times, and statistics are printed about it.
The {list of statistics, list of individual runtimes} are returnd.

Example:
benchmark[{n=RandomInteger[10^3,10^5]},FactorInteger[n],times->10]

Options:
times:9 # to run the expression and collect runtime data
printStats:True Wether to print statistics about the distribution of times.
";

(* Compare performance of two expressions *)
compareRunTimes[locals_,base_,comp_,opts:OptionsPattern[]]:=Module[
{timeLst,baseTimeLst,compTimeLst,totalTime,ratio,times=OptionValue[times],runtimes=0,llocs},
progressBar[Dynamic[runtimes],times];
totalTime=AbsoluteTiming[
    timeLst=Map[
        With[locals,
            Evaluate[ReleaseHold@OptionValue[init]];runtimes=#;
            {ReleaseHold@AbsoluteTiming[base][[1]],
             ReleaseHold@AbsoluteTiming[comp][[1]]}]&,
        Range[times]]][[1]];
{baseTimeLst,compTimeLst}=Transpose@timeLst;
If[OptionValue[printStats],
    ratio=(Plus @@ compTimeLst)/(Plus @@ baseTimeLst);
    Quiet[styledPrint[{HoldForm[comp], Style[" (right)",Blue], " took ", Style[(ToString@NumberForm[100. ratio,3]) <> "%", If[ratio>1,Red,Green]], " as much time to evaluate as much as ", HoldForm[base], Style[" (left)", Blue], "!"},24,Bold],{NumberForm::sigz}];
    styledPrint[{"Stats for time ratios for ",times," runs of '", HoldForm[comp], "' and '", HoldForm[base] ,"':"},Bold,20];
    statsSummary[compTimeLst/baseTimeLst,total->False];
    styledPrint[{"Stats for ",times," runs of '", HoldForm[base] ,"':"},Bold,20];
    statsSummary[baseTimeLst];
    styledPrint[{"Stats for ",times," runs of '", HoldForm[comp] ,"':"},Bold,20];
    statsSummary[compTimeLst];];
Return[{baseTimeLst,compTimeLst}]
];

Options[compareRunTimes]={printStats->True,times->9,init->Null};
SetAttributes[compareRunTimes,HoldAll];
compareRunTimes::usage="compareRunTimes[locals_,base_,comp_,OptionsPattern[]] takes a list of localized var=val assignments (which are evaluated anew each run) (lexical, via Module). The expressions base and comp is evaluated the given number of times (with the same local calculated values for both), and statistics are printed about it.
The {list of statistics, list of individual runtimes} are returnd.

Example:
compareRunTimes[{s=RandomInteger[10^5,100000]},FactorInteger[s],FactorInteger[s (s+1)],times\[Rule]10]

Options:
times:9 # to run the expression and collect runtime data
printStats:True Wether to print statistics about the distribution of times.
";
share|improve this answer
up vote 69 down vote accepted

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

Then, make some definitions and wrap the profiled code in RuntimeTools`Profile

For example, in debug mode, run

f[x_] := x^2

Table[f[x], {100000}]; // RuntimeTools`Profile

and you get a nice

Mathematica graphics

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

share|improve this answer
1  
Any idea, why test[] := Module[{}, Print["Here"]; Pause[0.1]; Print["There"]; Pause[1]; Return[2]] and test[] // RuntimeTools`Profile does not work, even though your example works correctly? – Ajasja Jul 2 '12 at 8:37
4  
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
1  
@Wizard Have you tried wrapping your entire code, definitions and all, in RuntimeTools`Profile? This seems to work for me. I suspect that certain profiling handles have to be silently inserted by this function for the timings to be gathered. Can you give me an example where this method fails? – Mr.Wizard Jan 12 '15 at 11:42
1  
My issues mentioned in the comment above seem to be resolved in Mathematica 10.1. I also think that Mr.Wizard's suggestion should work in older versions, but I am not sure if I recall that correctly. Sorry for the late reply. – Wizard Jul 9 '15 at 11:13
2  
@Wizard for the first time, I realize that there are at least two Wizards living on this site:-) – matheorem Oct 11 '15 at 8:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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