develooper Front page | perl.perl6.users | Postings from November 2018

More efficient of two coding styles?

Thread Next
From:
Richard Hainsworth
Date:
November 16, 2018 03:55
Subject:
More efficient of two coding styles?
Message ID:
6b3f767e-62ef-c5b1-5416-0717cc620770@gmail.com
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' }

     }

}

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About