require "benchmark/ips" require "concurrent" class MutexFixnum def initialize @value = 0 @lock = Mutex.new end def increment(amount = 1) @lock.synchronize { @value += amount } end def value=(val) @lock.synchronize { old = @value @value = val old } end end Benchmark.ips do |x| [::MutexFixnum, ::Concurrent::AtomicFixnum].each do |klass| x.report(klass) do |count| sc = klass.new count.times { sc.increment } end x.report("thread-#{klass}") do |count| sc = klass.new t = [] 10.times { t << Thread.new do count.times { sc.increment } end } t.each(&:join) end end end