Created
April 23, 2014 16:04
-
-
Save phuesler/11221423 to your computer and use it in GitHub Desktop.
Testing how big the overhead of ruby timeout is
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' | |
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