Dispatch
Details
- The use of Dispatch will never affect results that are obtained but may make the application of long lists of rules much faster.
- Lists of rules are usually scanned sequentially when you evaluate an expression like expr/.rules. Rules such as a[1]->a1 and a[2]->a2, which cannot simultaneously apply, need not both be scanned explicitly. Dispatch generates a dispatch table which uses hash codes to specify which sets of rules need actually be scanned for a particular input expression.
- Lists of rules produced by assignments made with = and := are automatically optimized with dispatch tables when appropriate.
- Dispatch is treated as a raw object by functions like AtomQ and for purposes of pattern matching.
- Normal converts a Dispatch to a list of rules.
Examples
open all close allBasic Examples (2)
Create a dispatch table from a list of rules:
rules = {a -> b, b -> c, c -> a, d -> e, e -> d};dispatch = Dispatch[rules]The result after replacements is the same:
{a, b, c, d, e} /. rules{a, b, c, d, e} /. dispatchDispatch may be much faster for a long list of rules:
Short[rules = Table[x[i] -> RandomInteger[{1, i}], {i, 10000}]]dispatch = Dispatch[rules]Timing[Table[x[i] /. rules, {i, 10000}];]Timing[Table[x[i] /. dispatch, {i, 10000}];]Scope (2)
Use Dispatch with pattern-based rules:
disp = Dispatch[{1 -> a, 2 -> b, c_ ? NumericQ -> ∞}]{1, 2, 3, Pi, Sin[x], Cos[3]} /. dispCreate a dispatch from an association:
Dispatch[<|a -> b, c -> d|>]See Also
Tech Notes
Related Guides
History
Introduced in 1991 (2.0) | Updated in 2014 (10.0)
Text
Wolfram Research (1991), Dispatch, Wolfram Language function, https://reference.wolfram.com/language/ref/Dispatch.html (updated 2014).
CMS
Wolfram Language. 1991. "Dispatch." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/Dispatch.html.
APA
Wolfram Language. (1991). Dispatch. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Dispatch.html
BibTeX
@misc{reference.wolfram_2026_dispatch, author="Wolfram Research", title="{Dispatch}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/Dispatch.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_dispatch, organization={Wolfram Research}, title={Dispatch}, year={2014}, url={https://reference.wolfram.com/language/ref/Dispatch.html}, note=[Accessed: 13-June-2026]}