分析 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
Is there a good way to profile code in Mathematica? I would like to be able to recurse (i.e., if I say f[a_] := b[a]
, then Profile[f[1]]
should give almost the same output as Profile[b[1]]
), but I'll settle for being able to do something like applying Timing
to every relevant subexpression. It would be nice if I didn't have to special-case things like Module
, but I'd like, e.g., Profile[Module[{x=1+2},x!]]
to give me an output like
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,Wolfram Workbench 确实有一个分析器,尽管 根据文档,输出并不完全符合您想要的形式。
我应该指出,Mr.Wizard 在评论中提出的问题 - 缓存的结果将导致不同的计时结果 - 也可以应用在配置文件中。
如果您想在 Mathematica 中专门做一些事情,您可以尝试以下操作:
如果您很高兴将输出设置为 {timing,output, input},而不是问题中指定的 {timing, input, output},那么您可以去掉
#[[{1,3,2}]]
位。编辑
因为我有工作台,这里是一个例子。我有一个包
AdvancedPlots
,其中包含一个函数CobwebPlot
(是的,该函数本身可以改进)。在调试模式下运行包
然后运行此笔记本
给出以下输出。
Yes, Wolfram Workbench does have a profiler, although according to the documentation the output isn't quite in the form you want.
I should note that the issue raised by Mr.Wizard in comments - that cached results will lead to different timing results - can also apply in profile.
If you wanted to do something exclusively in Mathematica, you could try something like:
If you were happy enough to have the output as {timing,output, input}, rather than {timing, input, output} as specified in your question, you could get rid of the
#[[{1,3,2}]]
bit.EDIT
Since I have Workbench, here is an example. I have a package
AdvancedPlots
which includes a functionCobwebPlot
(and yes, the function itself could improved).Running the package in Debug mode
And then running this notebook
Gives the following output.
这是使用
TraceScan
为各个评估步骤计时的尝试。它使用原始的AbsoluteTime[]
增量,这可能是好是坏,具体取决于您实际期望的时间。确保在新内核上运行此示例,否则
Prime
将缓存其结果,并且所有计时都将 ~= 0。This is an attempt to use
TraceScan
to time individual steps of evaluation. It uses rawAbsoluteTime[]
deltas which could be good or bad depending on what you actually expect to time.Make sure to run this example on a fresh kernel, or
Prime
will cache its results and all timings will be ~= 0.正如贝利撒留在回答我上面链接的问题时所显示的那样,似乎 Wolfram Workbench 包含一个分析器。不过我不使用 Workbench,所以我无法详细说明它的用法。
As belisarius showed in answer to the question I linked above, it appears that Wolfram Workbench includes a profiler. I do not use Workbench however, so I cannot detail its use.