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

There are some gains to be had. This'll run in about 1/3rd the time of ikegami's version above for 1e6 elements, and proportionally more as the size increases:

my @sums; for my $i ( 0 .. $#data ) { $sums[ 0 ] += $data[$i][ 0 ]; $sums[ 1 ] += $data[$i][ 1 ]; $sums[ 2 ] += $data[$i][ 2 ]; $sums[ 3 ] += $data[$i][ 3 ]; } $sums[ $_ ] /= @data for 0 .. 3;

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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^3: Averaging Elements in Array of Array
by neversaint (Deacon) on Dec 26, 2008 at 13:30 UTC
    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.......
      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