Skip to content

Instantly share code, notes, and snippets.

@SpringMT
Last active August 29, 2015 14:06
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 SpringMT/5d1eb25234c5426ca87c to your computer and use it in GitHub Desktop.
Save SpringMT/5d1eb25234c5426ca87c to your computer and use it in GitHub Desktop.
thread bench for Ruby
require 'benchmark'
r = 1_000_000
rsqr = r*r
iteration = 10_000
threads= []
mutex = Mutex.new
cnt = [0, 0]
puts Benchmark.measure {
16.times do
t = Thread.new do
iteration.times do
x = rand(2*r) - r
x = x*x
y = rand(2*r) - r
y = y*y
i = (x+y) / rsqr
mutex.synchronize do
cnt[i] = cnt.at(i) + 1
end
end
end
threads << t
end
threads.each do |t|
t.join
end
num = cnt[0] * 4
den = iteration * 16
puts Benchmark::CAPTION
puts num / den.to_f
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment