Skip to content

Instantly share code, notes, and snippets.

@bernd
Created October 8, 2013 07:59
Show Gist options
  • Save bernd/6881191 to your computer and use it in GitHub Desktop.
Save bernd/6881191 to your computer and use it in GitHub Desktop.
Benchmark Array vs. ThreadSafe::Array
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