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