Dispatch
Usage
• Dispatch[ -> , -> , ... ]用来生成一个法则列表的一个最优化的分派表表示.由 Dispatch产生的这个对象可以用来给出 expr /. rules中的法则.
Notes
• Dispatch的使用绝不会影响所得到的结果,但可以使长的法则列表应用更快. • 当你对一个象 expr /. rules的表达式赋值时,法则列表通常按顺序被检索. 诸如 a[1]->a1和 a[2]->a2的法则不能同时使用,从而不必都明确被检索.Dispatch 生成一个分派表,对于特定的输入表达式,它使用哈希码来指明法则的哪些集合不必真正被检索. • 使用= 和:= 进行赋值所产生的法则列表在适当的时候会自动使用分派表来优化.
Further Examples
Here is a list of 1000 rules.
In[1]:=
|
Out[1]//Short=
|
This sets up a dispatch table that make these rules faster to use.
In[2]:=
|
You can apply either ordinaryrules or dispatchrules using /., the slash-dot operator (ReplaceAll).
In[3]:=
|
Out[3]=
|
The dispatch rules go through all the rules significantly faster. (The results depend on the platform that you are using.)
In[4]:=
|
Out[4]=
|
In[5]:=
|
Out[5]=
|
Now we make the table of rules four times as long and we again set up the corresponding dispatch table.
In[6]:=
|
While the time to do replacements with ordinaryrules increases, the time using dispatchrules is not much more than before.
In[7]:=
|
In[8]:=
|
Out[8]=
|
In[9]:=
|
Out[9]=
|
We clear the definitions that we made.
In[10]:=
|
|