Try this. It does it in 1 pass and creates only 1 temporary list as opposed to 2 half passes, 3 temporary lists (5 if you count the ranges inside the slices), and 2 temporary arrays, so I think it should work out to be faster.
I realise that I am omitting the lists used to create the hashes, but we both use one, so that balances out.
I've been trying to benchmark the two variations, but for reasons I haven't yet fathomed, I have yet to succeed. I'll update if I do.
sub remove_item { my ($i, %del) = (0); @del{@_} = undef; @items = grep { $i++ and !exists $del{$_} and $index -= ($i < $ind +ex) } @items; }
Update:I got the benchmarking to go.
#! perl -sw use strict; use Benchmark; my @i = qw( a b c x d e f a g h i j k l z m n o p q r t s t b u v w x +y z ); my @dups = qw( x a z t b ); my $index = 13; my @items = @i; sub remove_item { my %del = map { $_ => 1 } @_; my @part_1 = grep { !exists $del{$_} } @items[0..$index]; my @part_2 = grep { !exists $del{$_} } @items[$index+1..$#items]; $index = $#part_1; @items = (@part_1, @part_2); } sub remove_items { my ($i, %del) = (0); @del{@_} = undef; @items = grep { $i++ and !exists $del{$_} and $index -= ($i < $ind +ex) } @items; } print "@dups\n"; @items = @i; $index = 13; print "$index: $items[$index] : @items\n"; remove_item @dups; print "$index: $items[$index] : @items\n"; print $/ x 2; print "@dups\n"; @items = @i; $index=13; print "$index: $items[$index] : @items\n"; remove_item @dups; print "$index: $items[$index] : @items\n"; print $/ x 2; Benchmark::cmpthese( 1000, { mine => sub { @items = @i; $index=13; remove_items @dups; }, yours => sub { @items = @i; $index=13; remove_item @dups; }, }); __DATA__ # Output C:\test>195796 x a z t b 13: l : a b c x d e f a g h i j k l z m n o p q r t s t b u v w x y z 9: l : c d e f g h i j k l m n o p q r s u v w y x a z t b 13: l : a b c x d e f a g h i j k l z m n o p q r t s t b u v w x y z 9: l : c d e f g h i j k l m n o p q r s u v w y Benchmark: timing 1000 iterations of mine, yours... mine: 0 wallclock secs ( 0.51 usr + 0.00 sys = 0.51 CPU) @ 19 +60.78/s (n=1000) yours: 1 wallclock secs ( 0.64 usr + 0.00 sys = 0.64 CPU) @ 15 +62.50/s (n=1000) Rate yours mine yours 1562/s -- -20% mine 1961/s 25% -- C:\test>
I agree that this isn't the way I would tackle the overall problem, but I enjoyed playing with it!
In reply to Re: round-robin on varying sequence
by BrowserUk
in thread round-robin on varying sequence
by dpuu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |