Archive for the ‘benchmark’ Category

snippet : A benchmark in ruby (II)

require "benchmark"
ITERATION = 100_000

def measure(&block)
  ITERATION.times { block.call }
end

puts("PLATFORM: #{PLATFORM}")

br = Benchmark.bmbm do |x|
  x.report("{ }") do
    measure { }
  end

  x.report("string <<") do
    host_string = ""
    measure { host_string << "A" }
  end

  x.report("string +=") do
    host_string = ""
    measure { host_string += "A" }
  end

  x.report("2**16") do
    measure { 2**16 }
  end

  x.report("File write") do
    File.open("benchmark_out.txt", File::CREAT 
                                 | File::TRUNC 
                                 | File::WRONLY) do |f|
      measure { f.puts "A" }
    end
  end

  x.report("File read") do
    File.open("benchmark_out.txt", "r") do |f|
      measure { f.read }
    end
  end

end

File.open("#{PLATFORM}_benchmarks.txt", File::CREAT 
                                        | File::TRUNC
                                        | File::WRONLY) do |bf|
  bf.puts br
end