in reply to RE: RE: RE: Re: All array elements the same?
in thread All array elements the same?
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.
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.sub is_list_homegeneous{ my @list=@_; my $val=shift @list; foreach(@list){ return 0 if($_ ne $val) } return 1; }
|
|---|
| 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 |