in reply to read data 1 byte at a time
#!/usr/bin/perl use strict; use warnings; use Benchmark qw /cmpthese/; use Fcntl qw /:DEFAULT :seek/; my $file = "/etc/passwd"; sysopen our $fh1 => $file, O_RDONLY or die $!; open our $fh2 => $file or die $!; open our $fh3 => $file or die $!; our ($c1, $c2, $c3) = (0, 0, 0); cmpthese -10 => { sysread => 'sysseek $fh1 => 0, SEEK_SET or die; $c1 = 0; $c1 ++ while sysread $fh1 => my $char, 1', readline => 'seek $fh2 => 0, SEEK_SET or die; local $/ = \1; $c2 = 0; $c2 ++ while defined (my $char = <$fh2>)', getc => 'seek $fh3 => 0, SEEK_SET or die; $c3 = 0; $c3 ++ while defined (my $char = getc $fh3)', }; die '$c1 is empty' unless $c1; die '$c2 is empty' unless $c2; die '$c3 is empty' unless $c3; die 'Unequal' unless $c1 == $c2 && $c2 == $c3; __END__ Rate sysread getc readline sysread 642/s -- -49% -52% getc 1249/s 94% -- -7% readline 1338/s 108% 7% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: read data 1 byte at a time
by BrowserUk (Patriarch) on Oct 28, 2003 at 20:43 UTC | |
by Abigail-II (Bishop) on Oct 28, 2003 at 22:11 UTC | |
by BrowserUk (Patriarch) on Oct 28, 2003 at 22:30 UTC | |
by Abigail-II (Bishop) on Oct 28, 2003 at 22:42 UTC |