Skip to content

Instantly share code, notes, and snippets.

@waj

waj/bm_lock.cr Secret

Created October 8, 2019 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waj/0c4b5835af088e8921fee4cc2c6006ed to your computer and use it in GitHub Desktop.
Save waj/0c4b5835af088e8921fee4cc2c6006ed to your computer and use it in GitHub Desktop.
Mutex benchmarks
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
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