Robert Klemme
2/28/2005 8:29:00 PM
"Florian Gross" <flgr@ccan.de> schrieb im Newsbeitrag
news:38hcsvF5n2uqqU1@individual.net...
> Mark Probert 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?
>>
>> ### open each file and copy it
>> def rby_cat
>> of = "plato.txt.rby"
>> clean(of)
>> off = File.new(of, "w+")
>> Dir["Plato/*.txt"].each do |f|
>> text = IO.readlines(f)
>> off.puts text
>> end
>> off.close
>> end
>
> def ruby_cat()
> of = "plato.txt.ruby"
> clean of
> File.open(of, "w") do |off|
> Dir.glob("Plato/*.txt") do |f|
> off << File.read(f)
> end
> end
> end
>
> You might get better performance by reading the files in 4096 byte blocks
> or something similar.
.... and binary possibly helps, too.
def stream_copy(in, out)
while ( b = in.read(4096) )
out.write b
end
end
def ruby_file_cat(in, out)
File.open(in, "rb") do |i|
File.open(out, "wb") {|o| stream_copy(i, o)}
end
end
Or similar.
robert