in reply to Re: Re: Opening file and checking for data
in thread Opening file and checking for data

Perl's I/O is buffered, so it does one I/O for every disk block regardless of which method you use.
#!/usr/bin/perl if ($ARGV[0] eq 'line') { print "Line-at-a-time\n"; while (<STDIN>) { print if /perl/; } } else { print "All at once\n"; my @arr = (<STDIN>); foreach (@arr) { print if /perl/; } }
On my system, the block size is 4096 bytes. On an 8K file with 128 lines, we see:
$ strace -e read /tmp/t29 line </tmp/t29.8192 >/dev/null
...
read(0, "This is a line that contains the"..., 4096) = 4096
read(0, "This is a line that contains the"..., 4096) = 4096
read(0, "", 4096)                       = 0

$ strace -e read /tmp/t29 slurp </tmp/t29.8192 >/dev/null
...
read(0, "This is a line that contains the"..., 4096) = 4096
read(0, "This is a line that contains the"..., 4096) = 4096
read(0, "", 4096)                       = 0
Still, the diamond operator takes some time to operate, so slurping is still probably faster, but not because of I/O.