in reply to Filling buckets

Benchmarked under 5.6 (500_000 iterations, 5-30 seconds), fixed and updated 3 Jan 18:45 CST:
Rate 2501 tilly2 merlyn dominus dws tilly eg io repson + fastsp1 danger2 fast fast_c 2501 5263/s -- -30% -36% -39% -46% -46% -60% -64% -66% + -68% -70% -73% -89% tilly2 7491/s 42% -- -10% -13% -23% -23% -43% -49% -52% + -55% -57% -61% -84% merlyn 8278/s 57% 11% -- -3% -15% -15% -37% -43% -47% + -50% -52% -57% -83% dominus 8569/s 63% 14% 4% -- -12% -12% -35% -41% -45% + -49% -50% -55% -82% dws 9709/s 84% 30% 17% 13% -- -0% -26% -33% -38% + -42% -44% -49% -80% tilly 9737/s 85% 30% 18% 14% 0% -- -26% -33% -37% + -42% -44% -49% -80% eg 13089/s 149% 75% 58% 53% 35% 34% -- -10% -16% + -22% -24% -32% -73% io 14599/s 177% 95% 76% 70% 50% 50% 12% -- -6% + -13% -15% -24% -69% repson 15552/s 195% 108% 88% 81% 60% 60% 19% 7% -- + -7% -10% -19% -67% fastsp1 16694/s 217% 123% 102% 95% 72% 71% 28% 14% 7% + -- -3% -13% -65% danger2 17271/s 228% 131% 109% 102% 78% 77% 32% 18% 11% + 3% -- -10% -64% fast 19194/s 265% 156% 132% 124% 98% 97% 47% 31% 23% + 15% 11% -- -60% fast_c 47847/s 809% 539% 478% 458% 393% 391% 266% 228% 208% + 187% 177% 149% --

Replies are listed 'Best First'.
Re: Re: Filling buckets
by dws (Chancellor) on Jan 03, 2001 at 12:10 UTC
    Good data, Fastolfe! It's fun to look back at the fragments and guess how well they compare against one another, and then have hard data to check your guesses against.

    In this particular case, though, I'll bet a round of virtual drinks that performance isn't an issue, and that the split-into-three-lists routine gets called no more than a handful of times per CGI invocation.

      Yeah I'd say performance probably doesn't really matter.

      If it really isn't an issue then it would be better to rate the solutions on efficiency, maintainability, flexibility and coolness.

      eg's solution may be the fastest in this set of benchmarks, but it doesn't include a variable number of buckets (which may be useful later in a project) and it isn't one the one that I personally (not begin a perl god) can understand and alter the fastest.

      Maybe someone else can come up with a better, and overall rating....

        I wasn't trying to say one person's code sucked over another's. I was just giving benchmark information. When we have a lot of code offered like that I like to see statistics like that sometimes. A good benchmark usually means the coder has a good understanding of what operations in Perl are more efficient, and took that into account when offering his solution. In my eyes that's the sign of a very good Perl coder!

        Any arbitrary "rating" taking other aspects of their code into account would be incredibly subjective and not very useful. Readability is in the eye of the beholder, so to speak, so take a look at each of them and decide for yourself which one is the most elegant in your eyes for the efficiency it gets.

Re: Re: Filling buckets
by Anonymous Monk on Jan 03, 2001 at 16:10 UTC

    Fastolfe, could you post your benchmark code? Perhaps I am not using Benchmark.pm correctly, but I do not obtain similar standings.
    thanks.

      It's rather long, but here it is. By all means let me know if I've done something wrong.

      I've removed the code because this node was really long and annoying. It's in the source code of the page if you want it, but it's out of date since others have been added since then.

        aha: I should've asked for this myself. A couple things wrong with it:

        1. the dws() routine shifts @array instead of @_, so all code that ran after it (everything but danger()) was passed an empty @array. This is the big source of differences.(dws() also needs fixing to return the actual buckets).
        2. the repson() routine does a my @array = shift when it should do a my @array = @_ (only affects its own standing).

        below are corrected dws() and repson() routines, and I've taken this opportunity to include a fix for my silly holdover in my danger() routine:

        sub dws { my $nbuckets = 3;#shift; # number of buckets to divide @array int +o my @bucket; # @{$bucket{0 .. $nbuckets - 1}} are the bucket +s foreach my $n( 0 .. $nbuckets - 1) { foreach my $size (1 .. int(0.9999 + @_ /($nbuckets - $n))){ push @{$bucket[$n]}, shift @_; } } return @bucket; } sub repson { my @array = @_; my $num = 3; # buckets my $cnt = @array; # total items my $base = int($cnt/$num); # } my $left = $cnt % $num; # } $cnt = ($base * $num) + $left my @buckets; for (1..$num) { push @buckets, [ splice(@array,0,$base + ($left-- > 0 && 1 +) ) ]; } @buckets; } sub danger_fixed { my $buckets = shift; my @list = @_; my $mod = @list % $buckets; my $inc = int(@list / $buckets); map{ [splice @list, 0, $inc + (--$mod >= 0)] } 0 .. $buckets - 1; } relative results: Rate dws tilly eg fast_sp2 tillyr danger repson fast_sp1 + danger1 fastolfe dws 1034/s -- -1% -18% -32% -34% -36% -36% -40% + -42% -52% tilly 1043/s 1% -- -17% -31% -33% -35% -35% -39% + -42% -52% eg 1263/s 22% 21% -- -17% -19% -21% -22% -27% + -30% -42% fast_sp2 1516/s 47% 45% 20% -- -3% -5% -6% -12% + -16% -30% tillyr 1564/s 51% 50% 24% 3% -- -3% -3% -9% + -13% -28% danger 1604/s 55% 54% 27% 6% 3% -- -1% -7% + -11% -26% repson 1613/s 56% 55% 28% 6% 3% 1% -- -6% + -10% -26% fast_sp1 1723/s 67% 65% 36% 14% 10% 7% 7% -- + -4% -21% danger1 1796/s 74% 72% 42% 18% 15% 12% 11% 4% + -- -17% fastolfe 2174/s 110% 108% 72% 43% 39% 36% 35% 26% + 21% --