-
Notifications
You must be signed in to change notification settings - Fork 32
Home
keita edited this page Sep 12, 2010
·
7 revisions
TimeoutX provides lightweight timeout function. TimeoutX.timeout is about 10 times faster than standard Timeout.timeout.
sudo gem install timeoutx
require "timeoutx"
TimeoutX.timeout(1){ ... }
To replace Timeout.timeout into TimeoutX.timeout:
require "timeoutx"
TimeoutX.replace_timeout
Timeout.timeout(1){ ... }
Sample benchmark results (2007/12/05) by examples/benchmark.rb :
n = 100000
Benchmark.bmbm(10) do |x|
x.report("Timeout") { 1.upto(n) do ; Timeout.timeout(1){true}; end }
x.report("TimeoutX") { 1.upto(n) do ; TimeoutX.timeout(1){true}; end }
end
Rehearsal ---------------------------------------------
Timeout 5.960000 0.460000 6.420000 ( 6.521520)
TimeoutX 0.780000 0.050000 0.830000 ( 0.851778)
------------------------------------ total: 7.250000sec
user system total real
Timeout 5.650000 0.790000 6.440000 ( 6.516992)
TimeoutX 0.760000 0.080000 0.840000 ( 0.863438)
Rehearsal ---------------------------------------------
Timeout 12.140000 2.760000 14.900000 ( 15.333607)
TimeoutX 0.490000 0.010000 0.500000 ( 0.518974)
----------------------------------- total: 15.400000sec
user system total real
Timeout 11.210000 2.390000 13.600000 ( 14.337455)
TimeoutX 0.490000 0.010000 0.500000 ( 0.512449)
Because TimeoutX keeps one thread for timeout countdown loop. Timeout.timeout creates a new thread when timeout function is called, for example, 10,000 threads are created if you call it 10,000 times. TimeoutX library is useful if you need to call timeout function many times in short period.
This code is free to use under the terms of the Ruby License.