Skip to content

Instantly share code, notes, and snippets.

@msumit
Created June 8, 2015 09:06
Show Gist options
  • Save msumit/c1452034c0dd482db012 to your computer and use it in GitHub Desktop.
Save msumit/c1452034c0dd482db012 to your computer and use it in GitHub Desktop.
benchmarking system vs File
require 'rubygems'
require 'benchmark'
require 'fileutils'
iterations = 1000
Benchmark.bm do |bm|
puts "write to a file using backticks"
bm.report do
iterations.times do
`echo testtesttesttest >> test1.file`
end
end
puts "write to a file using file method"
bm.report do
iterations.times do
File.open('test2.file', 'a'){|f| f.puts('testtesttesttest')}
end
end
puts "create and remove a file using backticks"
bm.report do
iterations.times do
`touch test3.file`
`rm -f test3.file`
end
end
puts "create and remove a file using file_utils"
bm.report do
iterations.times do
FileUtils.touch('test4.file')
File.delete('test4.file')
end
end
puts "move a file using backticks"
bm.report do
iterations.times do
`mv test1.file test5.file`
`mv test5.file test1.file`
end
end
puts "move a file using file"
bm.report do
iterations.times do
FileUtils.move('test2.file', 'test6.file')
FileUtils.move('test6.file', 'test2.file')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment