[ANN] threadify-0.0.3

A

ara howard

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://rubyforge.org/projects/codeforpeople

SAMPLES

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

~ > cat sample/a.rb

require 'open-uri'
require 'yaml'

require 'rubygems'
require 'threadify'


uris =
%w(
http://google.com
http://yahoo.com
http://rubyforge.org
http://ruby-lang.org
http://kcrw.org
http://drawohara.com
http://codeforpeople.com
)


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://codeforpeople.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

[ANN] threadify-0.0.1 18
[ANN] bj-0.0.3 0
[ANN] dike-0.0.3 2
[ANN] flatulent-0.0.3 21
[ANN] forkoff-0.0.4 0
[ANN] forkoff - parallel processing for ruby enumerables 20
[ANN] forkoff-0.0.1 0
[ANN] forkoff-1.1.0 0

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top