Nearly all the performance notes about Clojure multimethods apply to
ClojureScript.

On Saturday, April 25, 2015, Alex Miller <a...@puredanger.com> wrote:

> I should say all of that is for Clojure and likely bears no similarity to
> the nuances in ClojureScript.
>
> On Saturday, April 25, 2015 at 9:39:06 PM UTC-5, Alex Miller wrote:
>>
>> On Saturday, April 25, 2015 at 9:33:18 AM UTC-5, Timur wrote:
>>>
>>> Hi everyone,
>>>
>>> There are situations where I want to dispatch functions using based on
>>> their certain properties. I can also use case statements instead but it
>>> looks more coupled and more change is required if I want to add new types.
>>>
>>
>> case is useful for the particular situation where you have a finite set
>> of compile-time constants that you are looking for (integers, characters,
>> etc). Then case will leverage a special JVM bytecode that performs a
>> *constant-time* look-up (not linear-time like checking a series of cond
>> conditions, etc). This is one of the two bytecode options (tableswitch,
>> rather than lookupswitch) underlying the Java switch/case feature.
>>
>> What I want to ask is if I need to avoid using multi-methods for
>>> performance reasons? I read somewhere that they are not really fast but the
>>> posts were old and the performance might have been improved in between.
>>> Should I favor case and cond branches instead of defmulti when I need
>>> performance?
>>>
>>
>> multimethods are slower than protocols - they must effectively invoke the
>> dispatch function, perform a linear search for a match across the cases,
>> and then invoke the actual implementation function. Protocols leverage JVM
>> internals to select the right implementation, then invoke it. However, the
>> search part of multimethods is cached, making that step fast after the
>> initial call. The last time I benchmarked multimethods with class dispatch
>> vs protocols on Java 1.8 + Clojure 1.7 alphas (can't remember which one), I
>> found multimethods were about 5x slower.
>>
>> If you want fastest type-based dispatch with open extension, then
>> protocols are the best.
>> If you want any other type of dispatch with open extension, then
>> multimethods are the best.
>> If you want a closed system of constant choices, then case is best
>> (constant-time lookup!)
>> If you want a closed system of arbitrary conditions, then cond is best.
>>
>> I have never compared the performance of cond vs multimethods for
>> something like this. My guess is that multimethods are faster as they
>> evaluate a single condition, then do a table lookup once cached vs
>> evaluating a series of conditions every time.
>>
>> Alex
>>
>>  --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> <javascript:_e(%7B%7D,'cvml','clojure@googlegroups.com');>
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> <javascript:_e(%7B%7D,'cvml','clojure%2bunsubscr...@googlegroups.com');>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com
> <javascript:_e(%7B%7D,'cvml','clojure%2bunsubscr...@googlegroups.com');>.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to