# Workbench Profile[] question

I previously posted this question on Mathgroup but received no response

I am using Workbench to profile my code.

Suppose I have 2 modules, let's call them A, B (they are too long to replicate here). While running my code, Module B is called repeatedly from A, say in a Do loop. Module A is called once. I would like to profile total usage of B (not just one instance), which rules out embedding each call to B in a Profile[] statement (as this generates a Profile output for each call).

I could, of course, profile A, but the information produced on B would be buried in between timing data pertaining to other parts of A (which for my long module A makes it virtually impossible to understand what's happening in B).

Is there a way to accomplish this?

-
Welcome to SE, Eric! –  user21 Feb 15 '12 at 14:55

If you can settle for anything other than Workbench's Profile, here is a simplistic profiler function with a fairly low latency:

ClearAll[profile];
SetAttributes[profile, HoldAll];
profile[code_] :=
Block[{$totalTime = 0, t, res, time,exclude}, SetAttributes[{time, exclude}, HoldAll]; exclude[cd_] := (t = AbsoluteTime[];res = cd;$totalTime -= AbsoluteTime[] - t; res);
time[cd_] :=
(t = AbsoluteTime[];res = cd; $totalTime += AbsoluteTime[] - t; res); {code,$totalTime}];


To illustrate how you can use it:

b[] := Do[Range[10], {100}];
aa[] :=
Module[{},
Pause[2];
Do[time@b[], {1000}];
Pause[3];
];


Note that I inserted time wrapper in the place in the code which I want to profile. All invocations of time will add up:

profile[aa[]]

(*
==> {Null, 0.0791}
*)


Note that profile has some overhead, but it seems to at least give the right order of magnitude for the result:

a[] :=
Module[{},
Pause[2];
time@Do[b[], {1000}];
Pause[3];
];

profile[a[]]

(*
==> {Null,0.0781250}
*)


The granularity may be not as good as in Profile, but in case it is not enough (meaning you are timing something very fast), you should try to wrap time around pieces of code where your very fast operation is repeated (like loops). You can wrap pieces inside those which you want to exclude, in the exclude wrapper. Obviously, the code or functions it uses should not use the symbols time and exclude for other purposes, but this is easy to fix by placing profile into a separate context / package.

-