I don't know if either one has had any specific optimizations applied to it.

I would go for the multi-dispatch form if there is no overlap of functionality.
I think this would have a higher likelihood of getting the subroutine
inlined, because the code will be smaller.

If you really care that much more about performance than code
readability, then you should benchmark both.
You should run the benchmark on every release as it may change with
new optimizations.

I would personally just go with whatever is easier to understand.
On Thu, Nov 15, 2018 at 9:55 PM Richard Hainsworth
<rnhainswo...@gmail.com> wrote:
>
> Suppose I have a sub called C<handle> that runs different code depending
> on the content of an argument.
>
> There are two styles in Perl 6 to code this and my question is whether
> one is more efficient (speed/memory) than the other.
>
> In this example, one relies on multiple dispatch, while the other an
> explicit control statement.
>
> Putting aside clarity considerations, is there any performance reason to
> prefer one style over another?
>
> Style 1
>
> multi sub handle( $node WHERE *.name ~~ 'one' ) {
>
>      'this is a wonderful sub'
>
> }
>
> multi sub handle( $node WHERE *.name ~~ 'two' ) {
>
>     'twas brillig and the slivy toves did gyre'
>
> }
>
>
> Style 2
>
> sub handle ( $node ) {
>
>      given $node.name {
>
>          when 'one' { 'this is a wonderful sub' }
>
>          when 'two' { 'twas brillig and the slivy toves did gyre' }
>
>      }
>
> }

Reply via email to