Oh well, if we're going for speed:

#!/usr/bin/perl use List::MoreUtils; use Perl6::Junction; use strict; use warnings; use Benchmark qw( cmpthese ); my @list = ('1')x10; sub equal_pm {@_ < 2 || $_[0] eq $_[1] && equal_pm(@_[1..$#_])} sub equal_i { return 1 if @_ < 2; return 0 if $_[0] ne $_[1]; shift; goto(&equal_i); } sub equal_i2 { while (@_ >= 2) { return 0 if $_[0] ne $_[1]; shift; } return 1; } sub equal_ti { return 1 if (Perl6::Junction::all(@_) eq $_[0]); return 0; } sub equal_fr { my $first = shift; for(@_) { return 0 unless $_ eq $first; } return 1; } sub equal_pw { return 1 if (List::MoreUtils::all { $_ eq $_[0] } @_); return 0; } sub equal_zx { my %hash; @hash{@_} = (); !(1 - keys %hash); } cmpthese(-3, { pm => sub { my $rv = equal_pm(@list) ?1:0; 1; }, i1 => sub { my $rv = equal_i (@list) ?1:0; 1; }, i2 => sub { my $rv = equal_i2(@list) ?1:0; 1; }, pw => sub { my $rv = equal_pw(@list) ?1:0; 1; }, ti => sub { my $rv = equal_ti(@list) ?1:0; 1; }, fr => sub { my $rv = equal_fr(@list) ?1:0; 1; }, zx => sub { my $rv = equal_zx(@list) ?1:0; 1; }, });
Which gives:
Rate pm ti i1 pw i2 fr zx pm 52188/s -- -30% -47% -69% -73% -78% -82% ti 74488/s 43% -- -24% -55% -61% -68% -74% i1 97929/s 88% 31% -- -41% -49% -58% -65% pw 166481/s 219% 123% 70% -- -13% -29% -41% i2 192200/s 268% 158% 96% 15% -- -18% -32% fr 235585/s 351% 216% 141% 42% 23% -- -17% zx 283834/s 444% 281% 190% 70% 48% 20% --
So friedo's solution (with some slight alterations for performance) wins.

Update:Included Zaxo's hash solution, all hail the new speed king!


Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan

In reply to Re^3: Testing the Uniformity of an Array by its Elements by tirwhan
in thread Testing the Uniformity of an Array by its Elements by neversaint

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.