Created
December 22, 2010 08:31
-
-
Save jamesmoriarty/751269 to your computer and use it in GitHub Desktop.
Cpu bound task threading benchmark.
This file contains 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 '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