in reply to RE: RE: RE: Re: All array elements the same?
in thread All array elements the same?

I agree, except in your assertion that the hash brute-force method is best.

Performance wise the hash method will always take O(n) time, even if the first and second elements of the lists are different. An appriach like what I present below can bail-out if it finds a mismatch before examining the entire list. Therefore giving it a better average and best-case performance for lists that don't have homogeneous contents.

sub is_list_homegeneous{ my @list=@_; my $val=shift @list; foreach(@list){ return 0 if($_ ne $val) } return 1; }
This is not as short and cool, but does have better algorithmic performance. You could tweak-out the overhead I incurred by copying the list if you wanted the utmost in speed. My technique is probably slightly slower worse-case (a homogeneous list), because it does do slightly more work.

Replies are listed 'Best First'.
RE: RE: RE: RE: RE: Re: All array elements the same?
by eLore (Hermit) on Aug 01, 2000 at 22:59 UTC
    Should I start stripping RE:'s out of the subject line?

    Anyway, I sit corrected regarding speed. At worst case, your code is ever-so-slightly slower than 0(n).

    Thanks so much, for the discussion. It's proven to be a learning experience! -eLore