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
About these ads

1 comment so far

  1. Pedro on

    Via pastie


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: