I've had a go at benchmarking the two methods now. I have assumed that the data is clean with no need to strip trailing spaces other than a newline which we can chomp. The benchmark seems to show that using a hash slice and subsequently sorting the keys is still more efficient than using %seen but YMMV depending on hardware etc.; I'm using Suse 10.0 OSS/Perl v5.8.7 on an AMD Athlon 2500+.

use strict; use warnings; use Benchmark qw(cmpthese); our @lines = <DATA>; chomp @lines; our $rsHashSlice = sub { my %uniques; @uniques{@lines} = (); my @sorted; push @sorted, $_ for sort keys %uniques; return @sorted; }; our $rsSeen = sub { my %seen; my @sorted; foreach (@lines) { push @sorted, $_ unless $seen{$_}++; } return @sorted; }; cmpthese(100000, { HashSlice => $rsHashSlice, Seen => $rsSeen }); __END__ black black black black black black black black black black black black black black black black blue blue blue blue blue blue blue blue blue green green green green green green green green green green grey grey grey grey iolet mauve mauve mauve mauve mauve mauve mauve mauve pink pink pink pink pink purple purple purple red red red red red red red red violet violet violet violet violet violet violet violet violet white white white white white white white yellow yellow yellow yellow yellow yellow yellow

produces

Rate Seen HashSlice Seen 18939/s -- -34% HashSlice 28571/s 51% --

I have returned a list in each case as that seems to be closer in essence to the print in the OP than the list reference I would normally use.

Cheers,

JohnGG


In reply to Re^3: removing duplicate lines by johngg
in thread removing duplicate lines 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.