#!/usr/bin/perl use strict; use Benchmark qw( cmpthese ); foreach my $file qw ( /dev/null /usr/share/dict/words /etc/passwd ) { open my $IN1, '<', $file or die "could not open $file"; my @list = <$IN1>; seek( $IN1, 0, 0 ); print "$file\n"; cmpthese( -5, { for_list => sub { my %counts = (); ++$counts{$_} for @list; die unless keys %counts == @list; }, while_list => sub { my $x = 0; my %counts = (); ++$counts{$_} while defined( $_ = $list[ $x++ ] ); die unless keys %counts == @list; }, for => sub { seek( $IN1, 0, 0 ); my %counts = (); ++$counts{$_} for <$IN1>; die unless keys %counts == @list; }, while => sub { seek( $IN1, 0, 0 ); my %counts = (); ++$counts{$_} while <$IN1>; die unless keys %counts == @list; }, whyfor => sub { seek( $IN1, 0, 0 ); my %counts = (); for ( ; defined( $_ = <$IN1> ) ; ) { ++$counts{$_}; } die unless keys %counts == @list; }, } ); }