Jas*_*oss 12 profiling wolfram-mathematica
有没有一种很好的方法来分析Mathematica中的代码?我希望能够递归(即,如果我说f[a_] := b[a]
,然后Profile[f[1]]
应该给出几乎相同的输出Profile[b[1]]
),但我会满足于能够做一些像应用于Timing
每个相关的子表达式.如果我没有像特殊情况这样的东西会很好Module
,但我想,比如,Profile[Module[{x=1+2},x!]]
给我一个类似的输出
Time Expression Result
0 1 1
0 2 2
0 1 + 2 3
0 x$1234 3
0 x$1234 ! 6
0 Module[{x=1+2},x!] 6
6
Run Code Online (Sandbox Code Playgroud)
是的,Wolfram Workbench确实有一个分析器,虽然根据文档,输出不是你想要的形式.
我应该注意到Mr.Wizard在评论中提出的问题 - 缓存结果将导致不同的时间结果 - 也可以应用于配置文件中.
如果您想在Mathematica中专门做一些事情,您可以尝试以下方法:
myProfile[fun_Symbol,inputs_List]:=
TableForm[#[[{1,3,2}]]&/@ (Join @@@ ({Timing[f[#]],#} & /@ inputs))]
Run Code Online (Sandbox Code Playgroud)
如果您足够高兴将输出设置为{时间,输出,输入},而不是问题中指定的{时间,输入,输出},则可以摆脱这#[[{1,3,2}]]
一点.
编辑
由于我有Workbench,这是一个例子.我有一个AdvancedPlots
包含一个函数的包CobwebPlot
(是的,函数本身可以改进).
CobwebPlot[x_?MatrixQ, opts___Rule] /;
And @@ (NumericQ /@ Flatten[x]) :=
Module[{n, \[Theta]s, numgrids, grids, struts, gridstyle, min, max,
data, labels, epilabels, pad},
n = Length[First[x]];
\[Theta]s = (2 \[Pi])/n Range[0, n] + If[OddQ[n], \[Pi]/2, 0];
numgrids =
If[IntegerQ[#] && Positive[#], #,
NumberofGrids /.
Options[CobwebPlot] ] & @ (NumberofGrids /. {opts});
{min, max} = {Min[#], Max[#]} &@ Flatten[x];
gridstyle = GridStyle /. {opts} /. Options[CobwebPlot];
pad = CobwebPadding /. {opts} /. Options[CobwebPlot];
grids =
Outer[List, \[Theta]s, FindDivisions[{0, max + 1}, numgrids]];
struts = Transpose[grids];
labels = CobwebLabels /. {opts} /. Options[CobwebPlot];
epilabels =
If[Length[labels] == n,
Thread[Text[
labels, (1.2 max) Transpose[{Cos[Most[\[Theta]s]],
Sin[Most[\[Theta]s]]}]]], None];
data = Map[Reverse,
Inner[List, Join[#, {First[#]}] & /@ x, \[Theta]s, List], {2}];
Show[ListPolarPlot[grids, gridstyle, Joined -> True, Axes -> False,
PlotRangePadding -> pad],
ListPolarPlot[struts, gridstyle, Joined -> True, Axes -> False],
ListPolarPlot[data,
Sequence @@ FilterRules[{opts}, Options[ListPolarPlot]],
Sequence @@
FilterRules[Options[CobwebPlot], Options[ListPolarPlot]],
Joined -> True, Axes -> None] ,
If[Length[labels] == n, Graphics /@ epilabels,
Sequence @@ FilterRules[{opts}, Options[Graphics]] ]]
]
Run Code Online (Sandbox Code Playgroud)
在调试模式下运行包
然后运行这个笔记本
给出以下输出.