# File.read Vs. File.sysread performance

**URL:** <https://rubytalk.org/t/file-read-vs-file-sysread-performance/2185>\
**Category:** ruby-talk\
**Created:** [7 October 2002 16:26 UTC](https://rubytalk.org/t/file-read-vs-file-sysread-performance/2185 "2002-10-07T16:26:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allen\_Mitchell](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Allen\_Mitchell](https://rubytalk.org/u/Allen_Mitchell)\
**Post date:** [7 October 2002 16:26 UTC](https://rubytalk.org/t/file-read-vs-file-sysread-performance/2185/1 "2002-10-07T16:26:27Z")

</div>

I tried the following piece of code on W2000 using a 2.4MB file

s = Time.new  
data = File.open(“xx.data”,“rb”).read  
e = Time.new  
p e-s

Time to perform 6.5 seconds  
Then I changed it to

s = Time.new  
data = File.open(“xx.data”,“rb”).sysread( File::stat(“xx.data”).size )  
e = Time.new  
p e-s

Time to perform .01 seconds… THATS RIGHT .01 seconds

This was using a stable snapshot from about a month ago. And yes, I know  
that sysread is a low level function, but I never expected an improvement of  
650X.

Any thoughts?

Allen Mitchell

---

<div class="post-metadata">

**Author:** ![Alan\_Chen2](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Alan\_Chen2](https://rubytalk.org/u/Alan_Chen2)\
**Post date:** [7 October 2002 17:05 UTC](https://rubytalk.org/t/file-read-vs-file-sysread-performance/2185/2 "2002-10-07T17:05:55Z")

</div>

Could it be that there are some file IO caching effects that you’re seeing here?  
At the very least, I’d reverse the order of the tests as well as have several  
different files (could be identical files) to test.

- alan

> **···**
>
> On Tue, Oct 08, 2002 at 01:26:27AM +0900, Allen Mitchell wrote:
> 
> > This was using a stable snapshot from about a month ago. And yes, I know  
> > that sysread is a low level function, but I never expected an improvement of  
> > 650X.
> 
> –  
> Alan Chen  
> Digikata LLC  
> [http://digikata.com](http://digikata.com)

---

<div class="post-metadata">

**Author:** ![Allen\_Mitchell](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Allen\_Mitchell](https://rubytalk.org/u/Allen_Mitchell)\
**Post date:** [7 October 2002 17:51 UTC](https://rubytalk.org/t/file-read-vs-file-sysread-performance/2185/3 "2002-10-07T17:51:50Z")

</div>

I think you are somewhat right. I created new files and then shutdown the  
system to clear the cache. On a retry, the times changed to .25 seconds and  
.02 seconds. I think that something else was causing the extreme delay on  
the first one.

Guess I should have tested more before commenting. Thanks for the comments.

> **···**
>
> -----Original Message-----  
> From: Alan Chen [[mailto:alan@digikata.com](mailto:alan@digikata.com)]  
> Sent: October 7, 2002 14:06  
> To: ruby-talk ML  
> Subject: Re: File.read Vs. File.sysread performance…
> 
> On Tue, Oct 08, 2002 at 01:26:27AM +0900, Allen Mitchell wrote:
> 
> > This was using a stable snapshot from about a month ago. And yes, I know  
> > that sysread is a low level function, but I never expected an improvement  
> > of  
> > 650X.
> 
> Could it be that there are some file IO caching effects that you’re seeing  
> here?  
> At the very least, I’d reverse the order of the tests as well as have  
> several  
> different files (could be identical files) to test.
> 
> - alan
> 
> –  
> Alan Chen  
> Digikata LLC  
> [http://digikata.com](http://digikata.com)
