Benchmarks

use strict; use warnings; my @array=qw(a b c d e); my $num = 13; sub jw { my $average = int($num/@array); my %result = map{$_=>$average} @array; for my $i (1..($num - $average*@array)){ ++$result{$array[$i-1]}; } %result } sub ike1 { my %result; my $n=$num; $result{$array[$n%@array]}++ whil +e ($n--); %result } sub ike1b { my %result; $result{$array[$_%@array]}++ for (1..$num); % +result } sub ike3 { my $i; map{$array[$i++],length}('1'x$num)=~/^@{['(.*)'.'( +\1.?)'x$#array]}$/ } sub zaxo { my %result; @result{@array} = map { int($num/@array) + ($ +_ < $num % @array) } 0 .. $#array; %result } sub davido { my %result; my @a=@array; my $n=$num; $result{do{my$v=shi +ft@a;push@a,$v;$v}}++while$n--; %result } sub ted { my %result; my $n=0; $result{$array[$n++]} = int($num / ( +$#array + 1)) + ($num % ($#array + 1) > $n) while ($n <= $#array); %r +esult } sub pela { my %result; foreach (1..$num) { $result{$array[int(rand(scalar(@array)))]}++; } $result{$_} ||= 0 foreach (@array); %result } use Benchmark (); Benchmark::cmpthese(0, { jw => \&jw, ike1 => \&ike1, ike3 => \&ike3, ike1b => \&ike1b, zaxo => \&zaxo, davido => \&davido, ted => \&ted, pela => \&pela, }); __END__
Rate davido ike3 pela jw ted ike1b zaxo ike1 davido 22603/s -- -50% -54% -66% -69% -70% -73% -73% ike3 45636/s 102% -- -8% -31% -37% -40% -45% -45% pela 49364/s 118% 8% -- -25% -32% -35% -40% -40% jw 65704/s 191% 44% 33% -- -9% -13% -20% -21% ted 72090/s 219% 58% 46% 10% -- -5% -12% -13% ike1b 75516/s 234% 65% 53% 15% 5% -- -8% -9% zaxo 82287/s 264% 80% 67% 25% 14% 9% -- -1% ike1 82719/s 266% 81% 68% 26% 15% 10% 1% --

In reply to Re: Seek one liner for distributing an integer by ikegami
in thread Seek one liner for distributing an integer by johnnywang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.