Skip to content

Instantly share code, notes, and snippets.

@jamesmoriarty
Created December 22, 2010 08:31
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 jamesmoriarty/751269 to your computer and use it in GitHub Desktop.
Save jamesmoriarty/751269 to your computer and use it in GitHub Desktop.
Cpu bound task threading benchmark.
require 'benchmark'
N_OPS = 100_000_000
N_THREADS = 10
BM_LABEL_WIDTH = 15
def func(n_ops)
(1..n_ops).each { 1 + 1 }
end
Benchmark.bm(BM_LABEL_WIDTH) do |x|
x.report("1 thread: ") do
func(N_OPS)
end
x.report("#{N_THREADS} threads: ") do
threads = []
(1..N_THREADS).each do |n|
threads << Thread.new { func(N_OPS / N_THREADS) }
end
threads.each { |thread| thread.join }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment