Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > sysread vs read?

Reply
Thread Tools

sysread vs read?

 
 
kj
Guest
Posts: n/a
 
      07-10-2007



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

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      07-10-2007
kj <> wrote:
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
kj
Guest
Posts: n/a
 
      07-11-2007
In <20070710192114.167$> writes:

>kj <> wrote:
>> 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.


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

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      07-11-2007
kj <> wrote:
> In <20070710192114.167$> writes:
>
> >kj <> wrote:
> >> 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.

>
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      07-11-2007
In <20070711124042.743$> writes:
>kj <> wrote:
>> 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.


Cool. Thanks!

kj

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
`sysread' end of file reached `rbuf_fill' problem Mento Ruby Ruby 0 12-11-2006 10:21 AM
IO#sysread on windows pihentagy Ruby 3 06-14-2006 10:33 PM
Timeout Error or sysread': end of file reached (EOFError) Muazzam Mushtaq Ruby 0 03-28-2006 05:05 AM
invalid arg to sysread deep within protocol.rb William E. Rubin Ruby 3 12-08-2005 09:53 PM
sysread and buffered I/O Hal Fulton Ruby 39 07-24-2004 03:45 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57