设计方法:重载与切换? [英] Design approach: Overloading vs Switch?

查看:134
本文介绍了设计方法:重载与切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于包装设计的性能和可伸缩性,最好是:

In regard to performance and scalability in package design, is it best to:

  1. …重载"函数名称(让Mathematica根据模式/条件/测试以及系统订购定义的方式来选择使用哪个版本)?
  2. …还是使用Switch [](或类似命令)构造单个函数以直接求值?

Mathematica的表现力经常使我困惑于此类愚蠢的(?)问题.

Mathematica’s expressiveness frequently confuses me with silly (?) issues like this.

推荐答案

这是一个广泛的问题,但是我将借此机会给出一个广泛的答案...

This is a broad question, but I will take this opportunity to give a broad answer...

我主张人们应该接受一种编程语言的主要范例,而不是试图与之抗衡或编写遵循另一种语言习惯用法的代码. Mathematica是围绕模式匹配的概念构建的,因此恕我直言,我们在尝试表达自己时应始终首先考虑模式匹配.遵循该原则,相对于Switch,我更倾向于定义.

I advocate that one should embrace the principal paradigm of a programming language, rather than trying to fight it or to write code that follows the idioms of another language. Mathematica is built around the notion of pattern-matching so, IMHO, we should always consider pattern-matching first when trying to express ourselves. Following that principle, I would favour definitions over Switch.

在性能问题上,当比较Mathematica结构时,对微基准的日益重视使我越来越烦恼.虽然知道与构造相关的成本很有价值,但我们应该留心Knuth(或者说是Hoare?):我们应该忘记效率低下的问题,大约有97%的时间是这样:过早的优化是万恶之源". 邪恶"是程序的可读性下降,为了提高效率,该程序使用了一些晦涩或间接的方法来实现效果.这是我的性能清单:

On the question of performance, I am becoming increasingly vexed by the growing emphasis on microbenchmarks when comparing Mathematica constructs. While it is valuable to know the costs associated with constructs, we should heed Knuth (or was it Hoare?): "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil". The "evil" being the loss of readability in a program that, in the interests of efficiency, uses some obscure or indirect approach to achieve an effect. Here is my performance checklist:

  1. 性能有问题吗?如果没有,则跳过清单的其余部分.

  1. Is performance a problem? If not, then skip the rest of the checklist.

性能瓶颈在哪里?探查器可以在这里提供帮助,但通常可以通过检查或一些打印语句轻松找到瓶颈.然后...

Where is the performance bottleneck? A profiler helps here, but often the bottleneck can be found easily by inspection or a few print statements. Then...

算法效率低下吗?经常出现:是否有一个双嵌套循环可以通过索引方案进行线性化或帮助?

Is the algorithm inefficient? Very frequently: is there a doubly-nested loop that could be linearized or helped out by an indexing scheme?

好的,算法很好,所以我想是时候进行微基准测试了.

Okay, the algorithm is good so I guess it is time to microbenchmark.

我不知道我对Mathematica的使用是否不够雄心勃勃,但是大多数时候我都没有走过第一步.然后#3抓住了其余大部分.在Mathematica中,我通常感到非常高兴,因为我可以用少量的代码执行一些雄心勃勃的任务-总体性能通常不会体现在图片中.

I don't know if my use of Mathematica is not ambitious enough, but most of the time I don't get past step #1. And then #3 catches most of the rest. In Mathematica, I find I'm usually just overjoyed that I can perform some ambitious task with a small amount of code -- overall performance usually doesn't enter into the picture.

嗯,我最好把肥皂盒丢掉.不好意思.

Uh-oh, I'd better put the soapbox away. Sorry 'bout that.

这篇关于设计方法:重载与切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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