That is jumping through some hoops to avoid void. SuprisinglyUnsuprisingly (I just looked at it properly) the first one is a lot slower than the map in void context solution, the last one is slowest of all :)

Updated

added the for loop solution to the benchmark, it is nicely faster.
#!/usr/bin/perl use strict; use warnings; use Benchmark; my @strings; my @array; push @strings, $_ x 2 for ("a" .. "z"); for (1..1000) { my $i=int rand $#strings; push @array, $strings[$i] } sub for_loop { my %sums; $sums{$_}++ foreach @array; # print "$_ = $sums{$_}\n" foreach sort keys %sums; } sub map_void { my %sums; map {$sums{$_}++} @array; # print "$_ = $sums{$_}\n" foreach sort keys %sums; } sub nonvoid_1 { my %sums; %sums = map {$_ => ++$sums{$_} } @array; # print "$_ = $sums{$_}\n" foreach sort keys %sums; } sub nonvoid_2 { my %sums; my $last=""; %sums = map { my $n; do { $n = (($last=$_) ... ($last ne $_)) } while $n =~ /E0$/; ($last => $n); } sort @array; # print "$_ = $sums{$_}\n" foreach sort keys %sums; } timethese( -3, {map_void => \&map_void, for_loop => \&for_loop, nonvoid_1 => \&nonvoid_1, nonvoid_2 => \&nonvoid_2 } ); __END__ Benchmark: running for_loop, map_void, nonvoid_1, nonvoid_2 for at lea +st 3 CPU seconds... for_loop: 3 wallclock secs ( 3.13 usr + 0.01 sys = 3.14 CPU) @ 15 +79.94/s (n=4961) map_void: 3 wallclock secs ( 3.19 usr + 0.00 sys = 3.19 CPU) @ 11 +65.83/s (n=3719) nonvoid_1: 3 wallclock secs ( 3.16 usr + 0.00 sys = 3.16 CPU) @ 33 +0.06/s (n=1043) nonvoid_2: 3 wallclock secs ( 3.28 usr + 0.00 sys = 3.28 CPU) @ 13 +9.33/s (n=457)

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

In reply to Re^3: Counting unique elements in an array by Random_Walk
in thread Counting unique elements in an array by Anonymous Monk

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.