File::read Performance

Hi there,

if I make the following implementation of the command “cp” with ruby, the
read Performance is really bad. Can I improve it in any way ?

Thx in advance,

Daniel.

···

copies source to destionation

def cp(source, dest)
Dir[source].each {|f|
if (File.directory?(dest)) then
outfilename=dest+"/"+File.basename(f)
else
outfilename=dest
end
puts outfilename

    inF = File.new(f, "r") #p rescue "ERROR: #$!"
    if (!inF) then
        printf("%s: Could not open output file %s, maybe insufficient

rights ?\n", PROGRAM_NAME, f)
inF.close()
next
end

    outF = File.new(outfilename, "w+")
    if (!outF) then
        printf("%s: Could not open output file %s, maybe insufficient

rights ?\n", PROGRAM_NAME, outfilename)
inF.close()
next
end

    # now read
    outF.binmode
    inF.binmode
    while (! inF.eof() )
        outF << inF.read(8192)
    end
    outF.close()
    inF.close()
    }

end

if I make the following implementation of the command "cp" with ruby, the
read Performance is really bad. Can I improve it in any way ?

ftool.rb has a method #copy, it use syread, syswrite

Guy Decoux

“Daniel Schnell” daniel.schnell@embeddedware.de writes:

Hi there,

if I make the following implementation of the command “cp” with ruby, the
read Performance is really bad. Can I improve it in any way ?

First, make sure you’re running ruby 1.6.7 (or is it 1.7) that has an
improved IO#read and IO#write.

Secondly, if you still think IO#read and IO#write are still too slow,
use IO#sysread and IO#syswrite.

Sometimes ago, I published a benchmark of ruby reading using
IO#read, and IO#sysread, againsts UNIX’s cat and perl’s read.

The result was IO#sysread was slightly faster than cat which was
faster than perl’s read which was faster than IO#read. Read more at:
https://rubytalk.org/42780

YS.