You are currently browsing the tag archive for the ‘Benchmark’ tag.
Update: you might like Time.benchmark
Usage
benchmark "deleting users" do User.all.each(&:destroy) end
Output
deleting users... took 123.3435 seconds
Script
def benchmark(what, &block)
require 'benchmark'
puts "#{what}..."
result = nil
time = Benchmark.realtime do
result = yield
end
puts "took #{time} seconds"
result
end
Have fun
The One imo not-so-obvious solution is:
And why it works is an exercise for the reader
Update:
A crazy benchmark comparing different versions of power of 2 algorithms by reto
The fastest, still readable versions is:
While working on my new Fast-Gettext i needed a really fast way load the current translations, since they are needed every time, and must be stored inside Thread.current for Thread-safety. So i came up with a small micro benchmark for fastest accessor.
This test is separate into single-access and generic access (just one value / n values)
Results
Ruby 1.8.7 generic: Symbol: 1.06s String concat: 1.73s String add: 1.69s String insert: 1.47s single: Symbol: 0.67s String: 0.96s
So as we can see, stick to symbols!
Test
require 'benchmark'
BASELINE = 0
def test
result = Benchmark.measure {1_000_000.times{ yield }}
result.to_s.strip.split(' ').first.to_f - BASELINE
end
BASELINE = (test{})
Thread.current[:library_name]={}
other = "x"
puts "generic:"
puts "Symbol: #{test{Thread.current[:library_name][:just_a_symbol]}}s"
puts "String concat: #{test{Thread.current["xxxxxx"<<other.to_s]}}s"
puts "String add: #{test{Thread.current["xxxxxx"+other.to_s]}}s"
puts "String insert: #{test{Thread.current["xxxxxx#{other}"]}}s"
puts "single:"
puts "Symbol: #{test{Thread.current[:long_unique_symbol]}}s"
puts "String: #{test{Thread.current["xxxxxx"]}}s"
