-
-
Save waj/0c4b5835af088e8921fee4cc2c6006ed to your computer and use it in GitHub Desktop.
Mutex benchmarks
This file contains 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" | |
i1 = 0_i64 | |
m1 = Mutex.new | |
spawn do | |
loop do | |
m1.synchronize do | |
i1 += 1 | |
end | |
end | |
end | |
Benchmark.ips do |x| | |
x.report("Mutex.synchronize") do | |
m1.synchronize do | |
i1 -= 1 | |
end | |
end | |
end |
This file contains 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
i = 0 | |
m = Mutex.new | |
ch = Channel(Nil).new | |
n = ARGV[0].to_i | |
4.times do | |
spawn do | |
n.times do | |
m.synchronize { i += 1 } | |
end | |
ch.send nil | |
end | |
end | |
start = Time.local | |
4.times { ch.receive } | |
finish = Time.local | |
puts finish - start | |
puts i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment