Mathematica中功能和模式匹配之间的性能差异 [英] Performance difference between functions and pattern matching in Mathematica

查看:137
本文介绍了Mathematica中功能和模式匹配之间的性能差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此Mathematica与lisp的其他方言不同,因为它模糊了函数和宏之间的界限.在Mathematica中,如果用户想编写数学函数,则他们可能会使用f[x_]:= x*x而不是f=Function[{x},x*x]之类的模式匹配,尽管在使用f[x]调用时两者都将返回相同的结果.我的理解是,第一种方法等效于lisp宏,根据我的经验,由于语法更简洁,因此受到青睐.

So Mathematica is different from other dialects of lisp because it blurs the lines between functions and macros. In Mathematica if a user wanted to write a mathematical function they would likely use pattern matching like f[x_]:= x*x instead of f=Function[{x},x*x] though both would return the same result when called with f[x]. My understanding is that the first approach is something equivalent to a lisp macro and in my experience is favored because of the more concise syntax.

所以我有两个问题,执行函数与模式匹配/宏方法之间是否存在性能差异?如果将功能实际上转换为某些版本的宏以允许实现Listable之类的功能,我的一部分不会感到惊讶.

So I have two questions, is there a performance difference between executing functions versus the pattern matching/macro approach? Though part of me wouldn't be surprised if functions were actually transformed into some version of macros to allow features like Listable to be implemented.

我关心这个问题的原因是由于最近的一系列问题 (1) (2)有关尝试在大型程序中捕获Mathematica错误的信息.如果大多数计算是根据函数定义的,那么在我看来,跟踪求值顺序和错误的产生源比尝试通过逐次应用宏重写输入后尝试捕获错误要容易得多/模式.

The reason I care about this question is because of the recent set of questions (1) (2) about trying to catch Mathematica errors in large programs. If most of the computations were defined in terms of Functions, it seems to me that keeping track of the order of evaluation and where the error originated would be easier than trying to catch the error after the input has been rewritten by the successive application of macros/patterns.

推荐答案

我对Mathematica的理解是,它是一个巨大的搜索替换引擎.所有函数,变量和其他赋值实际上都存储为规则,并且在评估过程中,Mathematica会遍历此全局规则库并应用它们,直到结果表达式停止更改为止.

The way I understand Mathematica is that it is one giant search replace engine. All functions, variables, and other assignments are essentially stored as rules and during evaluation Mathematica goes through this global rule base and applies them until the resulting expression stops changing.

因此,您遍历规则列表的次数越少,评估越快.查看使用Trace会发生什么(使用 gdelfino 的函数g和h)

It follows that the fewer times you have to go through the list of rules the faster the evaluation. Looking at what happens using Trace (using gdelfino's function g and h)

In[1]:= Trace@(#*#)&@x
Out[1]= {x x,x^2}
In[2]:= Trace@g@x
Out[2]= {g[x],x x,x^2}
In[3]:= Trace@h@x
Out[3]= {{h,Function[{x},x x]},Function[{x},x x][x],x x,x^2}

显而易见,为什么匿名函数最快,为什么使用Function会比简单的SetDelayed引入更多的开销.我建议您查看Leonid Shifrin的绝妙著作的简介,其中这些概念是详细解释.

it becomes clear why anonymous functions are fastest and why using Function introduces additional overhead over a simple SetDelayed. I recommend looking at the introduction of Leonid Shifrin's excellent book, where these concepts are explained in some detail.

有时我会构造一个 Dispatch 表,其中我需要的功能,并将其手动应用到我的起始表达式中.由于Mathematica的内置函数都不需要与我的表达式相匹配,因此与常规评估相比,速度有了显着提高.

I have on occasion constructed a Dispatch table of all the functions I need and manually applied it to my starting expression. This provides a significant speed increase over normal evaluation as none of Mathematica's inbuilt functions need to be matched against my expression.

这篇关于Mathematica中功能和模式匹配之间的性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆