Skip to content

Instantly share code, notes, and snippets.

@phuesler
Created April 23, 2014 16:04
Show Gist options
  • Save phuesler/11221423 to your computer and use it in GitHub Desktop.
Save phuesler/11221423 to your computer and use it in GitHub Desktop.
Testing how big the overhead of ruby timeout is
require 'benchmark'
require 'timeout'
N = 10_000
def just_loop
N.times do |i|
i + 1
end
end
def span_threads
N.times do |i|
t = Thread.start do
i + 1
end
# make sure there is only one thread
t.join
end
end
def with_timeout
N.times do |i|
Timeout::timeout(1) do
i + 1
end
end
end
Benchmark.bm(7) do |x|
x.report("threads") { span_threads }
x.report("with timeout"){ with_timeout }
x.report("just loop") { just_loop }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment