# Catting files

**URL:** <https://rubytalk.org/t/catting-files/16753>\
**Category:** ruby-talk\
**Created:** [28 February 2005 20:17 UTC](https://rubytalk.org/t/catting-files/16753 "2005-02-28T20:17:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Probert](https://avatars.discourse-cdn.com/v4/letter/m/d2c977/32.png) [@Mark\_Probert](https://rubytalk.org/u/Mark_Probert)\
**Post date:** [28 February 2005 20:17 UTC](https://rubytalk.org/t/catting-files/16753/1 "2005-02-28T20:17:03Z")

</div>

Hi ..

There is approximately an order of magnitude difference in the performance of  
these two snippets. Is there a faster way of doing the pure Ruby version?

### use the system call 'cat'  
def sys\_cat  
&nbsp;&nbsp;&nbsp;&nbsp;of = "plato.txt.cat"  
&nbsp;&nbsp;&nbsp;&nbsp;clean(of)  
&nbsp;&nbsp;&nbsp;&nbsp;l = `cat Plato/*.txt \> #{of}`  
end

### open each file and copy it  
def rby\_cat  
&nbsp;&nbsp;&nbsp;&nbsp;of = "plato.txt.rby"  
&nbsp;&nbsp;&nbsp;&nbsp;clean(of)  
&nbsp;&nbsp;&nbsp;&nbsp;off = File.new(of, "w+")  
&nbsp;&nbsp;&nbsp;&nbsp;Dir["Plato/\*.txt"].each do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text = IO.readlines(f)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;off.puts text  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;off.close  
end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
sys\_cat 0.000000 0.015625 0.117188 ( 0.167577)  
rby\_cat 0.937500 0.085938 1.023438 ( 1.064247)

Thanks,

> **···**
>
> --  
> -mark. (probertm at acm dot org)

---

<div class="post-metadata">

**Author:** ![Alexander\_Kellett2](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Alexander\_Kellett2](https://rubytalk.org/u/Alexander_Kellett2)\
**Post date:** [28 February 2005 20:37 UTC](https://rubytalk.org/t/catting-files/16753/2 "2005-02-28T20:37:21Z")

</div>

IO.read?  
gets(nil)?

> **···**
>
> On Feb 28, 2005, at 9:17 PM, Mark Probert wrote:
> 
> > Hi ..
> > 
> > There is approximately an order of magnitude difference in the performance of  
> > these two snippets. Is there a faster way of doing the pure Ruby version?
> > 
> > ### use the system call 'cat'  
> > def sys\_cat  
> > &nbsp;&nbsp;&nbsp;&nbsp;of = "plato.txt.cat"  
> > &nbsp;&nbsp;&nbsp;&nbsp;clean(of)  
> > &nbsp;&nbsp;&nbsp;&nbsp;l = `cat Plato/*.txt \> #{of}`  
> > end
> > 
> > ### open each file and copy it  
> > def rby\_cat  
> > &nbsp;&nbsp;&nbsp;&nbsp;of = "plato.txt.rby"  
> > &nbsp;&nbsp;&nbsp;&nbsp;clean(of)  
> > &nbsp;&nbsp;&nbsp;&nbsp;off = File.new(of, "w+")  
> > &nbsp;&nbsp;&nbsp;&nbsp;Dir["Plato/\*.txt"].each do |f|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text = IO.readlines(f)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;off.puts text  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;off.close  
> > end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> > sys\_cat 0.000000 0.015625 0.117188 ( 0.167577)  
> > rby\_cat 0.937500 0.085938 1.023438 ( 1.064247)
> > 
> > Thanks,
> > 
> > --  
> > -mark. (probertm at acm dot org)

---

<div class="post-metadata">

**Author:** ![Javier\_Valencia](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Javier\_Valencia](https://rubytalk.org/u/Javier_Valencia)\
**Post date:** [28 February 2005 21:01 UTC](https://rubytalk.org/t/catting-files/16753/3 "2005-02-28T21:01:03Z")

</div>

Mark Probert wrote:

> Hi ..
> 
> There is approximately an order of magnitude difference in the performance of these two snippets. Is there a faster way of doing the pure Ruby version?
> 
> ### use the system call 'cat'  
> def sys\_cat  
> &nbsp;&nbsp;&nbsp;of = "plato.txt.cat"  
> &nbsp;&nbsp;&nbsp;clean(of)  
> &nbsp;&nbsp;&nbsp;l = `cat Plato/*.txt \> #{of}`  
> end
> 
> ### open each file and copy it  
> def rby\_cat  
> &nbsp;&nbsp;&nbsp;of = "plato.txt.rby"  
> &nbsp;&nbsp;&nbsp;clean(of)  
> &nbsp;&nbsp;&nbsp;off = File.new(of, "w+")  
> &nbsp;&nbsp;&nbsp;Dir["Plato/\*.txt"].each do |f|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text = IO.readlines(f)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;off.puts text  
> &nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;off.close  
> end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> sys\_cat 0.000000 0.015625 0.117188 ( 0.167577)  
> rby\_cat 0.937500 0.085938 1.023438 ( 1.064247)
> 
> Thanks,

Try mine:

def rby\_cat  
&nbsp;&nbsp;&nbsp;&nbsp;File.open("file.rby", "w+") do |file|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dir["\*.txt"].each do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file.write(IO.readlines(f))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [28 February 2005 21:02 UTC](https://rubytalk.org/t/catting-files/16753/4 "2005-02-28T21:02:27Z")

</div>

def faster\_cat  
&nbsp;&nbsp;&nbsp;&nbsp;of = "plato.txt.fct"  
&nbsp;&nbsp;&nbsp;&nbsp;clean(of)  
&nbsp;&nbsp;&nbsp;&nbsp;File.open(of, "wb+") do |outf|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dir["Plato/\*.txt"].each do |inf|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;outf.puts IO::read(inf)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

As I don't have the "Plato/\*.txt" files, I can't test it, but that  
you're not splitting the file into multiple lines and iterating over  
them will be faster.

-austin

> **···**
>
> On Tue, 1 Mar 2005 05:17:03 +0900, Mark Probert \<probertm@acm.org\> wrote:
> 
> > There is approximately an order of magnitude difference in the  
> > performance of these two snippets. Is there a faster way of doing  
> > the pure Ruby version?
> 
> > ### use the system call 'cat'  
> > def sys\_cat  
> > &nbsp;&nbsp;of = "plato.txt.cat"  
> > &nbsp;&nbsp;clean(of)  
> > &nbsp;&nbsp;l = `cat Plato/*.txt \> #{of}`  
> > end
> 
> --  
> Austin Ziegler \* halostatue@gmail.com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca

---

<div class="post-metadata">

**Author:** ![Mark\_Probert](https://avatars.discourse-cdn.com/v4/letter/m/d2c977/32.png) [@Mark\_Probert](https://rubytalk.org/u/Mark_Probert)\
**Post date:** [28 February 2005 21:20 UTC](https://rubytalk.org/t/catting-files/16753/5 "2005-02-28T21:20:50Z")

</div>

Hi ..

> **···**
>
> On Monday 28 February 2005 13:02, Austin Ziegler wrote:
> 
> > ```
> > of = &quot;plato\.txt\.fct&quot;
> > clean\(of\)
> > File\.open\(of, &quot;wb\+&quot;\) do |outf|
> > Dir\[&quot;Plato/\*\.txt&quot;\]\.each do |inf|
> > outf\.puts IO::read\(inf\)
> > end
> > end
> > 
> > ```
> 
> Perfect!  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> sys\_cat 0.000000 0.007812 0.101562 ( 0.164781)  
> rb\_cat 0.953125 0.070312 1.023438 ( 1.065408)  
> faster\_cat 0.046875 0.078125 0.125000 ( 0.166651)
> 
> Thanks, Austin. I knew there was a better way 🙂
> 
> --  
> -mark. (probertm at acm dot org)
