Take the 2-minute tour ×

First, I know that when copying a file, if the buffer size is bigger, the copying process will be faster.

The following ruby script used to write line-by-line (i.e. size of buffer will depends on the line length):

input,output = ARGV[0], ARGV[1]

f_in = File.open input, "r"
f_out = File.open output, "w"

f_in.each {|line| f_out << line}


Ruby Script VS to Linux cp command

I've tried to copy a two files of size (300, 400 MBs), and found the above script is even faster than the cp Linux command, notably faster.

How this could happen? I suppose the cp is fully optimized!

share|improve this question
Which one did you run first? Could be your disk cache at work. –  Thilo Sep 29 '13 at 6:13
And further to Thilo's comment, you don't just run things once to benchmark them, you need to run thousands of iterations and flush caches to get worthwhile numbers. –  mu is too short Sep 29 '13 at 17:23

2 Answers 2

up vote 2 down vote accepted

It's probably due disk caches, try running

echo 3 > /proc/sys/vm/drop_caches

to clear up the cache before running your benchmark :)

my guess is that they do should be very, very close, ofcourse cp should start copying some miliseconds faster than ruby.

share|improve this answer
I gonna re-becnmark and share the reuslts –  Muhammad Hewedy Sep 29 '13 at 16:27

It probably is happening because of the file system disk cache inside the kernel.

I'm sure that if you repeat several times a cp command, timings will be different.

Use the time command (as a prefix) to benchmark and repeat the test several times.

share|improve this answer
I agree, the cp command is very faster in subsequent times. –  Muhammad Hewedy Sep 29 '13 at 16:27

Your Answer


