Last active
October 4, 2024 16:25
-
-
Save mperham/5cffb51d6eaee6a9b07e17bb0661a69b to your computer and use it in GitHub Desktop.
Atomic counters: MutexFixnum vs concurrent-ruby's AtomicFixnum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| jruby 9.4.8.0 (3.1.4) 2024-07-02 4d41e55a67 OpenJDK 64-Bit Server VM 18.0.2.1+0 on 18.0.2.1+0 +jit [arm64-darwin] | |
| Warming up -------------------------------------- | |
| MutexFixnum 1.399M i/100ms | |
| thread-MutexFixnum 88.529k i/100ms | |
| Concurrent::AtomicFixnum | |
| 8.141M i/100ms | |
| thread-Concurrent::AtomicFixnum | |
| 127.454k i/100ms | |
| Calculating ------------------------------------- | |
| MutexFixnum 13.902M (± 0.7%) i/s (71.93 ns/i) - 69.965M in 5.032978s | |
| thread-MutexFixnum 823.398k (± 5.3%) i/s (1.21 μs/i) - 4.161M in 5.067168s | |
| Concurrent::AtomicFixnum | |
| 79.494M (± 0.9%) i/s (12.58 ns/i) - 398.895M in 5.018339s | |
| thread-Concurrent::AtomicFixnum | |
| 1.268M (± 7.6%) i/s (788.80 ns/i) - 6.373M in 5.060407s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| % ruby bench.rb | |
| ruby 3.3.3 (2024-06-12 revision f1c7b6f435) [arm64-darwin23] | |
| Warming up -------------------------------------- | |
| MutexFixnum 860.528k i/100ms | |
| thread-MutexFixnum 85.405k i/100ms | |
| Concurrent::AtomicFixnum | |
| 341.332k i/100ms | |
| thread-Concurrent::AtomicFixnum | |
| 33.790k i/100ms | |
| Calculating ------------------------------------- | |
| MutexFixnum 8.588M (± 0.6%) i/s - 43.026M in 5.010274s | |
| thread-MutexFixnum 856.323k (± 0.9%) i/s - 4.356M in 5.086868s | |
| Concurrent::AtomicFixnum | |
| 3.411M (± 0.1%) i/s - 17.067M in 5.003070s | |
| thread-Concurrent::AtomicFixnum | |
| 336.522k (± 0.5%) i/s - 1.690M in 5.020613s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment