Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: Averaging Elements in Array of Array

by neversaint (Deacon)
on Dec 26, 2008 at 13:30 UTC ( #732674=note: print w/replies, xml ) Need Help??


in reply to Re^2: Averaging Elements in Array of Array
in thread Averaging Elements in Array of Array

Dear BrowserUk,
Your method is fast indeed. Is there a way I can modify your code so that in can incorporate any length of sub-array size? (i.e > 4 elements).

---
neversaint and everlastingly indebted.......
  • Comment on Re^3: Averaging Elements in Array of Array

Replies are listed 'Best First'.
Re^4: Averaging Elements in Array of Array
by BrowserUk (Pope) on Dec 26, 2008 at 14:06 UTC
    Is there a way I can modify your code so that in can incorporate any length of sub-array size?

    Not really. The basis of the main gain is the well-known optimisation called Loop unrolling.

    As with most optimisations, it relies on trading application specific knowledge for generic programing practices. As soon as you put back the generic loop:

    my @sums; for my $ref ( @data ) { $sums[ $_ ] += $ref->[ $_ ] for 0 .. $#$ref; } $sums[ $_ ] /= @data for 0 .. 3;

    You lose most of the gains.

    If your application really calls for best speed, but the size of the sub-array can vary from run to run, then using a dynamic language like Perl does offer one possibility not available with non-dynamic languages. That of code generation.

    You could eval a sub into existance at runtime that unrolls the loop to deal with the right number of subarray elements for that particular run, giving you the best of both worlds. Genericity and application specific knowledge.

    Whether that is worth the extra complexity and maintenance effort will depend upon the specifics of your application. If you really need the performance, it is worth considering.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      BrowserUk,
      I wanted to point to a good example of code generation but it is not something I see here often. I know of some test suites that automatically generate thousands of tests but that feels a bit too artificial. I rarely use the technique so other than When should I use a dispatch table? (hardly "good") or some examples of automatic method generation, I couldn't come up with any. Do you have any pointers to practical code generation (other than compiler optimizers)?

      Cheers - L~R

        Do you have any pointers to practical code generation (other than compiler optimizers)?

        "Practical" is a value judgement, an area where my judgement seems to vary widely with the concensus, so you'll have to make up your own mind.

        In terms of the OP's question, I consider this practical. It runs a gnat's appendage slower than a hardcoded unrolled loop, but is 'generic':

        sub aveAoA { my( $ref, $n ) = @_; my @sums; my $code = qq[ for my \$i ( 0 .. \$#\$ref ) { ]; $code .= qq[ \$sums[ $_ ] += \$ref->[ \$i ][ $_ ];] for 0 .. $n -1 +; $code .= '}'; eval $code; $sums[ $_ ] /= @{ $ref } for 0 .. $n -1; return @sums; } our $M ||= 1e6; our $N ||= 4; my @data; $#data = $M -1; $data[ $_ ] = [ map int(-10+rand 20), 1.. $N ] for 0 .. $#data; my @sums = aveAoA( \@data, $N );

        The eval'd code is determanistic, and in my opinion totally 'safe'. Effectively, it is no different from selectively loading an appropriate subroutine (via import) at runtime except that it avoids having to have pre-written subroutines for every possible contingency.

        For existing, well-known and popular modules that already use runtime code generation, see CGI, Catalyst, Template::Toolkit, Moose (though you have to drill down a few levels), Test::More etc.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re^4: Averaging Elements in Array of Array
by Anonymous Monk on Dec 26, 2008 at 14:09 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://732674]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2020-02-25 18:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (112 votes). Check out past polls.

    Notices?