Permalink
Browse files

Added simple benchmarks

  • Loading branch information...
1 parent 0c7896b commit 18bb1ba1d1dc2cd0c10bff2ea7d4eb91451d3d37 @zimbatm zimbatm committed Aug 15, 2011
Showing with 34 additions and 0 deletions.
  1. +5 −0 Rakefile
  2. +29 −0 test/bench_atomic.rb
View
@@ -9,6 +9,11 @@ Rake::TestTask.new :test do |t|
t.test_files = FileList["test/**/*.rb"]
end
+desc "Run benchmarks"
+task :bench do
+ exec "ruby -Ilib -Iext test/bench_atomic.rb"
+end
+
if defined?(JRUBY_VERSION)
require 'ant'
View
@@ -0,0 +1,29 @@
+require 'benchmark'
+require 'atomic'
+require 'thread'
+
+N = 100_000
+@lock = Mutex.new
+@atom = Atomic.new(0)
+
+Benchmark.bm(10) do |x|
+ x.report "simple" do
+ value = 0
+ N.times do
+ value += 1
+ end
+ end
+ x.report "mutex" do
+ value = 0
+ N.times do
+ @lock.synchronize do
+ value += 1
+ end
+ end
+ end
+ x.report "atomic" do
+ N.times do
+ @atom.update{|x| x += 1}
+ end
+ end
+end

0 comments on commit 18bb1ba

Please sign in to comment.