11
$\begingroup$

I have a long list of rules for manipulating expressions. They are of the form:

complexRules = {f[x_, y_]^2 :> (*something*),
                f[x_, y_] f[x_, z_] :> (*something*),
                f[x_, y_] f[w_, z_] :> (*something*),
                g[x_, y_, z_]^2 :> (*something*),
                g[x_, y_, z_] g[a_, b_, z_]  :> (*something*),
                (*and so on*)}

As you can see, the LHS of the rules are fairly complicated patterns. I don't fully understand the construction and uses of such dispatch tables. But my hunch is that they wouldn't benefit from being Dispatched as compared to "static" (patternless) rules like

simpleRules = {anna :> (*something*),
               bebe :> (*something*),
               cecy :> (*something*),
               deedee :> (*something*),
               eli :> (*something*),
               (*and so on*)}

Is my hunch correct in that complexRules would benefit less from Dispatch than would simpleRules? And if so, by how much?

$\endgroup$
  • 6
    $\begingroup$ Since you are actually using long lists of patterns, you are in a good position to benchmark this! $\endgroup$ – Szabolcs Dec 27 '15 at 15:32
  • 4
    $\begingroup$ What @Szabolcs is trying to say is that the mob demands a benchmark :) $\endgroup$ – Sascha Dec 27 '15 at 17:19
  • $\begingroup$ What did you end up finding for this? The benchmark would be useful to have out there for a number of tests. $\endgroup$ – b3m2a1 Jun 6 '19 at 18:41
8
$\begingroup$

Dispatch generates a dispatch table which uses hash codes to specify which sets of rules need actually be scanned for a particular input expression reducing to a minimum lookup time. Lists of rules produced by assignments made with = and := are automatically optimized with dispatch tables when appropriate.

Example

Excerpt From: Mangano, Sal. “Mathematica Cookbook.” O'Reilly Media, 2010-04-02. iBooks. This material may be protected by copyright. Check out this book on the iBooks Store: https://itun.es/us/ApcQx.l

Example

Excerpt From: Mangano, Sal. “Mathematica Cookbook.” O'Reilly Media, 2010-04-02. iBooks. This material may be protected by copyright. Check out this book on the iBooks Store: https://itun.es/us/ApcQx.l

Below is the performance benefit of using a Dispatch:

Ratio

99.8 % decrease in CPU time used by the Wolfram Kernel.

CR

I tried on two different systems and the performance ratio is very similar to the one shown in the book:

complexRepeatedEvaluation

$\endgroup$
  • $\begingroup$ That is some good information about how Dispatch works. Thanks! Would you know if Dispatch boosts performance for a list of complicated rules (which have quite intricate patterns) just as much as for simple ones (like in the book)? $\endgroup$ – QuantumDot Dec 27 '15 at 16:19
  • 2
    $\begingroup$ @QuantumDot I think you should add this to the body of the question because it is the key point. One one addition: simple rules with patterns are not the same as rules with Condition or PatternTest! $\endgroup$ – Alexey Popkov Dec 27 '15 at 19:16
  • $\begingroup$ I believe it does improve performance. You are right that doesn't include the difficulties of testing and conditions. I think yours is an interesting question. It could be useful if you include a working example to work with or what is happening on your machine. I ask myself these types of questions before as well and it always helps to bring a working example. $\endgroup$ – Schopenhauer Dec 27 '15 at 19:26
  • 2
    $\begingroup$ @QuantumDot Why can't you test it on your set of rules? $\endgroup$ – Michael E2 Dec 27 '15 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.