Re: More efficient of two coding styles?

2018-11-17 Thread Parrot Raiser
On 11/15/18, Richard Hainsworth  wrote:

> There are two styles in Perl 6 to code this and my question is whether
> one is more efficient (speed/memory) than the other.
>
First, define efficiency.
Which is cheaper, computer time or programmer time?
Is whatever is being considered a constraint of any kind, or is it fast enough?

We can use interpreted scripts to cat files, because the display takes
so much more time than the interpretation, while graphics operations
have to run so fast, so often,, they require C compiled for special
processors.


Re: More efficient of two coding styles?

2018-11-16 Thread Brad Gilbert
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
 wrote:
>
> Suppose I have a sub called C 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' }
>
>  }
>
> }


Re: More efficient of two coding styles?

2018-11-15 Thread Brent Laabs
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 
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 
> wrote:
>
>> Suppose I have a sub called C 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' }
>>
>>  }
>>
>> }
>>
>


Re: More efficient of two coding styles?

2018-11-15 Thread Brent Laabs
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 
wrote:

> Suppose I have a sub called C 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' }
>
>  }
>
> }
>


More efficient of two coding styles?

2018-11-15 Thread Richard Hainsworth
Suppose I have a sub called C 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' }

    }

}