#!/usr/bin/perl use strict; use warnings; use Benchmark qw/cmpthese/; my $time = shift; for my $file (@ARGV) { my $binary = -B $file; printf "Processing a %s file, %d bytes long\n", $binary ? 'binary' : 'text', -s $file; cmpthese($time, { 'read' => sub{open_read($file, $binary)}, 'sysread' => sub{open_sysread($file, $binary)}, 'slurp' => sub{open_slurp($file, $binary)}, }); print "\n"; } sub open_read { my ($file, $binary) = @_; open IT, $file or die "Can't open $file: $!"; binmode IT if $binary; read IT, my $buffer, -s IT; close IT; } sub open_sysread { my ($file, $binary) = @_; open IT, $file or die "Can't open $file: $!"; binmode IT if $binary; sysread IT, my $buffer, -s IT; close IT; } sub open_slurp { my ($file, $binary) = @_; open IT, $file or die "Can't open $file: $!"; binmode IT if $binary; my $buffer = do {local $/, }; close IT; } __END__