Skip to content
keita edited this page Sep 12, 2010 · 7 revisions

TimeoutX

TimeoutX provides lightweight timeout function. TimeoutX.timeout is about 10 times faster than standard Timeout.timeout.

Installing


sudo gem install timeoutx

Usage


require "timeoutx" 

TimeoutX.timeout(1){ ... }

To replace Timeout.timeout into TimeoutX.timeout:


require "timeoutx" 
TimeoutX.replace_timeout

Timeout.timeout(1){ ... }

Benchmark

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

ruby 1.8.6


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)

ruby 1.9.0


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)

Why TimeouX is faster than standard timeout?

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.

License

This code is free to use under the terms of the Ruby License.