----- Original Message -----
From: "Will W" <[EMAIL PROTECTED]>

>
> Everyone's fascination with sysread() got me to poking around in the
> camel and cookbook a bit-- and I still can't see the advantage here of
> doing a low-level system call over using read(), which is generally
> buffered for optimal efficiency on whatever system your using. sysread()
> makes a lot of sense on a port, but on a disk file I'm not seeing the
> why of it-- maybe someone will enlighten me.
>

To have a closer look at 'read' versus 'sysread' I did a benchmark test.

I found that 'sysread' took (almost exactly) twice as long as 'read'.

So that was a pretty pertinent point you raised, Will.
Guess I'd have to agree that I, too, don't see the advantage of using
sysread in preference to read.

I also ran the same code using the trick with '$/' (which is also mentioned
in perlvar, btw). Not surprisingly it's pretty much the same speed as
'read'.

Here's a cut'n'paste of both the code I ran and the result I got. Sorry if
it gets wrapped inconveniently.

use Benchmark;

timethese (1, {

'sysread' => 'open (READ, "d:/pscrpt/tab.new")
         or die "Cannot open READ: $!";
    binmode READ;

    while (sysread(READ, $read, 4) == 4) {
             $mod = unpack("I", $read);
             }

    print "$mod\n";

    close (READ) or die "Cannot close READ: $!";',


'read' => 'open (READ, "d:/pscrpt/tab.new")
         or die "Cannot open READ: $!";
    binmode READ;

    while (read(READ, $read, 4) == 4) {
             $mod = unpack("I", $read);
             }
    print "$mod\n";

    close (READ) or die "Cannot close READ: $!";',

'local' => '{
             local $/;
             $/ = \4;
open (READ, "d:/pscrpt/tab.new")
         or die "Cannot open READ: $!";
    binmode READ;

    while (<READ>) {
             $mod = unpack("I", $_);
             }
    print "$mod\n";

    close (READ) or die "Cannot close READ: $!";
    }',
});

##########################################

Benchmark: timing 1 iterations of local, read, sysread...
27112421
     local: 25 wallclock secs (24.90 usr +  0.09 sys = 24.98 CPU) @  0.04/s
 (n=1)
            (warning: too few iterations for a reliable count)
27112421
      read: 26 wallclock secs (26.42 usr +  0.10 sys = 26.52 CPU) @  0.04/s
 (n=1)
            (warning: too few iterations for a reliable count)
27112421
   sysread: 53 wallclock secs (34.20 usr + 17.39 sys = 51.59 CPU) @  0.02/s
 (n=1)
            (warning: too few iterations for a reliable count)

Cheers,
Rob

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to