Created
October 8, 2013 07:59
-
-
Save bernd/6881191 to your computer and use it in GitHub Desktop.
Benchmark Array vs. ThreadSafe::Array
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 'thread' | |
require 'thread_safe' | |
require 'benchmark' | |
core = [] | |
ts = ThreadSafe::Array.new | |
mutex = Mutex.new | |
iterations = 5_000 | |
threads = 1000 | |
Benchmark.bm do |x| | |
x.report('core') do | |
threads.times.map do |i| | |
Thread.new do | |
iterations.times do | |
mutex.lock | |
core << i | |
mutex.unlock | |
end | |
end | |
end.each(&:join) | |
end | |
x.report('ts') do | |
threads.times.map do |i| | |
Thread.new do | |
iterations.times do | |
ts << i | |
end | |
end | |
end.each(&:join) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment