Skip to content

Instantly share code, notes, and snippets.

@colinsurprenant
Created May 10, 2015 22:10
Show Gist options
  • Save colinsurprenant/72709a5c410656030759 to your computer and use it in GitHub Desktop.
Save colinsurprenant/72709a5c410656030759 to your computer and use it in GitHub Desktop.
performance comparison of basic volatile updates

Test run using:

  • JRuby 1.7.20
  • Java 1.8.0_45
  • OSX 10.10.3
  • MBP 13r 2.8 GHz Intel Core i7
Calculating -------------------------------------
         synchronize   190.964k i/100ms
                lock   198.430k i/100ms
              atomic   197.997k i/100ms
              direct   278.351k i/100ms
-------------------------------------------------
         synchronize      4.635M (± 5.9%) i/s -    138.449M
                lock      5.402M (± 5.9%) i/s -    161.522M
              atomic      5.464M (± 5.8%) i/s -    163.348M
              direct     15.324M (± 8.1%) i/s -    456.217M

Comparison:
              direct: 15324498.3 i/s
              atomic:  5463820.7 i/s - 2.80x slower
                lock:  5401972.6 i/s - 2.84x slower
         synchronize:  4634833.2 i/s - 3.31x slower
require "bundler/setup"
require "benchmark/ips"
require "concurrent_ruby"
@data = true
@mutex = Mutex.new
@atomic = Concurrent::AtomicBoolean.new(true)
def mutex_synchronize
@mutex.synchronize{@data}
end
def mutex_lock
@mutex.lock
@data
ensure
@mutex.unlock
end
def atomic
@atomic.value
end
def direct
@data
end
Benchmark.ips do |x|
x.warmup = 30
x.time = 30
mutex = Mutex.new
data = true
x.report("synchronize") do
mutex_synchronize
end
x.report("lock") do
mutex_lock
end
x.report("atomic") do
atomic
end
x.report("direct") do
direct
end
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment