Skip to content

Instantly share code, notes, and snippets.

@chuckremes
Created March 26, 2014 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuckremes/9777120 to your computer and use it in GitHub Desktop.
Save chuckremes/9777120 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'benchmark/ips'
small_file = File.join(File.dirname(__FILE__), 'small.txt')
medium_file = File.join(File.dirname(__FILE__), 'medium.txt')
large_file = File.join(File.dirname(__FILE__), 'large.txt')
# Open these before the actual benchmarks so we don't also benchmark
# File.open().
small_handle = File.open(small_file, 'r')
medium_handle = File.open(medium_file, 'r')
large_handle = File.open(large_file, 'r')
small_handle2 = File2.open(small_file, 'r')
Benchmark.ips do |x|
x.report "File#each_byte with a small file" do
small_handle.each_byte {}
small_handle.rewind
end
x.report "File2#each_byte with a small file" do
small_handle2.each_byte {}
small_handle2.rewind
end
x.report "File#each_byte with a medium file" do
medium_handle.each_byte {}
medium_handle.rewind
end
x.report "File#each_byte with a large file" do
large_handle.each_byte {}
large_handle.rewind
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment