Skip to content

Instantly share code, notes, and snippets.

@yaauie
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save yaauie/0222292680254797b02b to your computer and use it in GitHub Desktop.

Select an option

Save yaauie/0222292680254797b02b to your computer and use it in GitHub Desktop.
def time_thread_contention(options = {})
thread_count = options.fetch(:threads, 10)
iterations = options.fetch(:iterations, 10)
start_time = Time.now
iterations.times.each_slice(thread_count).to_a.transpose.map do |idxs|
Thread.new do
idxs.each do |idx|
yield
Thread.pass if idx.odd?
end
end
end.map(&:join)
end_time = Time.now
end_time - start_time
end
def time_thread_contention_compensated(options = {}, &block)
with = time_thread_contention(options, &block)
without = time_thread_contention(options) { true }
with - without
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment