#!/usr/bin/perl use strict; use warnings; my $i = 1; for my $file (@ARGV) { open_read($file, $_) for (0,1); } sub open_read { my ($file, $binary) = @_; my $file_size = -s $file; open IT, $file or die "Can't open $file: $!"; binmode IT if $binary; my $handle_size = -s IT; my $read_len = read IT, my $buffer, -s IT; close IT; printf "%d) %5s (%s): File size %5d, Read length %5d,\n", $i++, $file, $binary ? 'b' : ' ', $file_size, $read_len; printf " : Handle size %5d, Buffer length %5d,\n", $handle_size, length $buffer } __END__ #### 1) dos1 ( ): File size 9, Read length 6, : Handle size 9, Buffer length 6, 2) dos1 (b): File size 9, Read length 9, : Handle size 9, Buffer length 9, 3) unix1 ( ): File size 6, Read length 6, : Handle size 6, Buffer length 6, 4) unix1 (b): File size 6, Read length 6, : Handle size 6, Buffer length 6, 5) p.exe ( ): File size 20069, Read length 4716, : Handle size 20069, Buffer length 4716, 6) p.exe (b): File size 20069, Read length 20069, : Handle size 20069, Buffer length 20069, #### 1) dos1 ( ): File size 9, Read length 9, : Handle size 9, Buffer length 9, 2) dos1 (b): File size 9, Read length 9, : Handle size 9, Buffer length 9, 3) unix1 ( ): File size 6, Read length 6, : Handle size 6, Buffer length 6, 4) unix1 (b): File size 6, Read length 6, : Handle size 6, Buffer length 6, 5) p.exe ( ): File size 20069, Read length 20069, : Handle size 20069, Buffer length 20069, 6) p.exe (b): File size 20069, Read length 20069, : Handle size 20069, Buffer length 20069, #### #!/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__ #### Processing a text file, 100 bytes long Benchmark: running read, slurp, sysread, each for at least 20 CPU seconds... read: 20 wallclock secs (20.07 usr + 0.00 sys = 20.07 CPU) @ 3179.19/s (n=63819) slurp: 20 wallclock secs (20.00 usr + 0.00 sys = 20.00 CPU) @ 3027.40/s (n=60551) sysread: 21 wallclock secs (20.88 usr + 0.00 sys = 20.88 CPU) @ 3411.66/s (n=71232) Rate slurp read sysread slurp 3027/s -- -5% -11% read 3179/s 5% -- -7% sysread 3412/s 13% 7% -- Processing a text file, 10000 bytes long Benchmark: running read, slurp, sysread, each for at least 20 CPU seconds... read: 20 wallclock secs (20.05 usr + 0.00 sys = 20.05 CPU) @ 2283.35/s (n=45772) slurp: 20 wallclock secs (20.11 usr + 0.00 sys = 20.11 CPU) @ 2135.80/s (n=42951) sysread: 21 wallclock secs (21.59 usr + 0.00 sys = 21.59 CPU) @ 2710.40/s (n=58531) Rate slurp read sysread slurp 2136/s -- -6% -21% read 2283/s 7% -- -16% sysread 2710/s 27% 19% -- Processing a binary file, 100 bytes long Benchmark: running read, slurp, sysread, each for at least 20 CPU seconds... read: 22 wallclock secs (21.59 usr + 0.00 sys = 21.59 CPU) @ 3012.83/s (n=65032) slurp: 21 wallclock secs (20.89 usr + 0.00 sys = 20.89 CPU) @ 2898.21/s (n=60558) sysread: 21 wallclock secs (20.15 usr + 0.00 sys = 20.15 CPU) @ 3173.55/s (n=63947) Rate slurp read sysread slurp 2898/s -- -4% -9% read 3013/s 4% -- -5% sysread 3174/s 10% 5% -- Processing a binary file, 100000 bytes long Benchmark: running read, slurp, sysread, each for at least 20 CPU seconds... read: 22 wallclock secs (21.43 usr + 0.00 sys = 21.43 CPU) @ 584.71/s (n=12531) slurp: 21 wallclock secs (21.04 usr + 0.00 sys = 21.04 CPU) @ 459.77/s (n=9673) sysread: 21 wallclock secs (21.03 usr + 0.00 sys = 21.03 CPU) @ 710.41/s (n=14937) Rate slurp read sysread slurp 460/s -- -21% -35% read 585/s 27% -- -18% sysread 710/s 55% 21% --