[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[ANN] threadify-0.0.3

ara.t.howard

7/12/2008 1:32:00 AM


NAME
threadify.rb

SYNOPSIS
enumerable = %w( a b c d )
enumerable.threadify(2){ 'process this block using two worker
threads' }

DESCRIPTION
threadify.rb makes it stupid easy to process a bunch of data using
'n'
worker threads

INSTALL
gem install threadify

URI
http://rub.../projects/cod...

SAMPLES

<========< sample/a.rb >========>

~ > cat sample/a.rb

require 'open-uri'
require 'yaml'

require 'rubygems'
require 'threadify'


uris =
%w(
http://...
http:/...
http://rub...
http://rub...
http:...
http://dra...
http://codefor...
)


time 'without threadify' do
uris.each do |uri|
body = open(uri){|pipe| pipe.read}
end
end


time 'with threadify' do
uris.threadify do |uri|
body = open(uri){|pipe| pipe.read}
end
end


BEGIN {
def time label
a = Time.now.to_f
yield
ensure
b = Time.now.to_f
y label => (b - a)
end
}

~ > ruby sample/a.rb

---
without threadify: 3.75206303596497
---
with threadify: 1.37899804115295


<========< sample/b.rb >========>

~ > cat sample/b.rb

require 'yaml'

require 'rubygems'
require 'threadify'

size = Integer(ARGV.shift || (2 ** 15))

haystack = Array.new(size){|i| i}
needle = 2 * (size / 3)

a, b = 4, 2

time 'without threadify' do
a = haystack.each{|value| break value if value == needle}
end

time 'with threadify' do
b = haystack.threadify(16){|value| threadify! value if value ==
needle}
end

raise if a != b

y :a => a, :b => b, :needle => needle

BEGIN {
def time label
a = Time.now.to_f
yield
ensure
b = Time.now.to_f
y label => (b - a)
end
}

~ > ruby sample/b.rb

---
without threadify: 0.00630998611450195
---
with threadify: 0.270262956619263
---
:needle: 21844
:a: 21844
:b: 21844


HISTORY
0.0.3
- added ability to short-circuit the parallel processing, a.k.a
to 'break'
from threadify

0.0.2
- don't use thread.exit, just let the thread die naturally
- add version to Threadify module
- comments ;-)



a @ http://codefor.../
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama