Micro Benchmark fastest threadsave Accessor

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"

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 )

Facebook photo

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

Connecting to %s