in reply to Averaging Elements in Array of Array

Well, it's trivial to do it in linear time, and obviously, you have to consider all elements, so there isn't much to win here.

Except perhaps doing it all in C.

  • Comment on Re: Averaging Elements in Array of Array

Replies are listed 'Best First'.
Re^2: Averaging Elements in Array of Array
by BrowserUk (Patriarch) on Dec 26, 2008 at 12:50 UTC

    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.
      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.