Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

benchmark for ivar access

  • Loading branch information...
commit 0b5bee059dda838cf3cab3e3dd52287a06cd6ae4 1 parent 67f4e58
@the8472 authored
Showing with 52 additions and 0 deletions.
  1. +52 −0 bench/core/basic_object/ivar_access_bench.rb
View
52 bench/core/basic_object/ivar_access_bench.rb
@@ -0,0 +1,52 @@
+require 'benchmark'
+
+
+SIMPLE_TIMES = 10_000_000
+GROWTH_TIMES = 8_000
+
+# prepare dummy objects
+objects = (0..20).map{Class.new.new}
+
+simple_reader = proc{|o| result = nil;SIMPLE_TIMES.times{ result = o.instance_variable_get(:@foo)};result}
+simple_writer = proc{|o| SIMPLE_TIMES.times{o.instance_variable_set(:@foo,1)}}
+
+
+Benchmark.bmbm do |b|
+ b.report "baseline x#{SIMPLE_TIMES}" do
+ SIMPLE_TIMES.times{1}
+ end
+
+ b.report "single threaded reads x#{SIMPLE_TIMES}" do
+ o = objects.pop;
+ o.instance_variable_set(:@foo,1)
+ simple_reader.call(o)
+ end
+
+ b.report "single threaded writes x#{SIMPLE_TIMES}" do
+ o = objects.pop
+ simple_writer.call(o)
+ end
+
+ b.report "two reader threads x#{SIMPLE_TIMES}" do
+ o = objects.pop;
+ o.instance_variable_set(:@foo,1)
+
+ t1 = Thread.new{simple_reader.call(o)}
+ t2 = Thread.new{simple_reader.call(o)}
+ t1.join;t2.join
+ end
+
+ b.report "one reader, one writer x#{SIMPLE_TIMES}" do
+ o = objects.pop
+ t1 = Thread.new{simple_reader.call(o)}
+ t2 = Thread.new{simple_writer.call(o)}
+ t1.join;t2.join
+ end
+
+ b.report "single threaded growth x#{GROWTH_TIMES}" do
+ o = objects.pop
+ (0..GROWTH_TIMES).each{|i| o.instance_variable_set(:"@bar_#{i}",1)}
+ end
+
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.