sysread vs read?

K

kj

I've rtfm'd this question but I still can't figure out why one
would prefer Perl's read over Perl's sysread, or viceversa. Is
one significantly faster than the other?

(And why would Perl provide two different functions that, aside
from possible differences in performance, seem to both do pretty
much the same thing?)

Any words o' wisdom would be much appreciated.

TIA!

kj
 
X

xhoster

kj said:
I've rtfm'd this question but I still can't figure out why one
would prefer Perl's read over Perl's sysread, or viceversa.

"read" can be used with readline (aka "<$fh>") , while sysread generally
can't be, at least not without introducing strange behavior.

sysread can be used with select (or IO::Select), while "read" generally
(or at least often) can't be.

Is
one significantly faster than the other?

If you use very small read sizes, then read will be much faster than
sysread. If you don't use small read sizes, then the main difference is in
what other functions/operations they are compatible with.

(And why would Perl provide two different functions that, aside
from possible differences in performance, seem to both do pretty
much the same thing?)

It would have been very hard to make readline compatible with sysread
(and select) without making readline very slow. So we have two
incompatible sets of features. "read" and "sysread" or analogous to each
other, but one in the buffered camp and one in the unbuffered camp.

Xho
 
K

kj

"read" can be used with readline (aka "<$fh>") , while sysread generally
can't be, at least not without introducing strange behavior.

Hmmm... I sense that I'm missing something important here...

The reason I'm fussing over read and sysread in the first place is
that I wanted to speed up some code that currently uses readline
to get data from a huge file (about 1.5GB). I thought that I could
speed things up by reducing the number of disk reads; i.e. by
periodically reading, say, 4096 bytes to a buffer in memory and
feeding lines to the program from this buffer...

But, reading your reply between the lines a bit, I'm now guessing
that maybe Perl already does precisely this behind the scenes even
when one uses readline, meaning that I would not be gaining much
speed, and may even lose some, by switching from my naive <$fh>
implementation to one using read or sysread. Did I get this right?

TIA!

kj
 
X

xhoster

kj said:
Hmmm... I sense that I'm missing something important here...

The reason I'm fussing over read and sysread in the first place is
that I wanted to speed up some code that currently uses readline
to get data from a huge file (about 1.5GB). I thought that I could
speed things up by reducing the number of disk reads; i.e. by
periodically reading, say, 4096 bytes to a buffer in memory and
feeding lines to the program from this buffer...

The disk reads issue is mostly likely at yet another level, well below
Perl. Even sysread is not likely to be issuing a physical disk-read for
each read.

But, reading your reply between the lines a bit, I'm now guessing
that maybe Perl already does precisely this behind the scenes even
when one uses readline, meaning that I would not be gaining much
speed, and may even lose some, by switching from my naive <$fh>
implementation to one using read or sysread. Did I get this right?

Yes, read and readline already do chunking.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top