I don't know the answer to your specific question -- maybe try benchmarking it? Subroutine dispatch is normally pretty fast, as are given statements. Both when and ~~ call the ACCEPTS method, just at different times in your examples. I'm not really sure if the JIT will kick in better in one case or another.
But this also seems to be in the sphere of premature optimization. You should write your program in a way that is most likely to give correct results, and allows you to be flexible where you need to be. There's More Than One Way To Do It. On Thu, Nov 15, 2018 at 8:26 PM Richard Hainsworth <rnhainswo...@gmail.com> wrote: > Brent, > > Thanks for the rapid response, but it does not really answer the intent of > the question. > > Essentially, you said that one style provides for a silent default, the > other does not. The intent of the question is about performance - as in the > subject line. > > Perhaps the word 'style' is mal-chosen, perhaps 'coding idiom' ? > > Style 1 can be adjusted to have an explicit default, eg. > > multi sub handle ( $node ) { 'I dont care' } > > Style 2 > > given .... { ... default { 'I dont care' } } > > > So, since these 'styles' / 'coding idioms' use different mechanisms to > achieve the same intended result (if necessary, I can re-work the examples > to get the same result), there must be different performance effects. > > Which coding idiom is better in performance terms? > > May be my question is "incorrect", in which case what is the implicit > assumption I am making that should be explicit? > > > On 16/11/2018 12:13, Brent Laabs wrote: > > They do two different things. Style 1 will throw a dispatch error if $ > node.name has a value of 'three', where Style 2 will do nothing in that > case. > > On Thu, Nov 15, 2018 at 7: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' } >> >> } >> >> } >> >