Re: threadify-0.0.1
by Daniel Berger other posts by this author
Jul 10 2008 7:45PM messages near this date
[ANN] threadify-0.0.1
|
Re: threadify-0.0.1
On Jul 1, 2:04Â pm, ara howard <ara.t.how...@[...].com> wrote:
> this one's for you charlie ;-)
>
> 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 installthreadify
>
> 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 'withoutthreadify' do
> Â Â Â Â uris.each do |uri|
> Â Â Â Â Â body = open(uri){|pipe| pipe.read}
> Â Â Â Â end
> Â Â Â end
>
> Â Â Â time 'withthreadify' do
> Â Â Â Â uris.threadifydo |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
>
> Â Â Â ---
> Â Â Â withoutthreadify: 7.41900205612183
> Â Â Â ---
> Â Â Â withthreadify: 3.69886112213135
Pretty cool. I tried it with file-find. Here was the code:
require 'file/find'
require 'threadify'
rule = File::Find.new(
:pattern => "*.rb",
:path => "C:\\ruby"
)
start = Time.now
rule.find.threadify(10){ |f|
p f
}
p start
p Time.now
Without threadify, it took 1:40 on my laptop. With threadify(10) it
dropped to 44 seconds.
I think I'll add a "threads" option directly, and borrow some of your
code. :)
Thanks,
Dan
Thread:
Ara Howard
Daniel Berger
Ara.T.Howard
Charles Oliver Nutter
Michael Guterl
Charles Oliver Nutter
Michael Guterl
Ara.T.Howard
Charles Oliver Nutter
Charles Oliver Nutter
Ara.T.Howard
Ara.T.Howard
Ara.T.Howard
Charles Oliver Nutter
Martin DeMello
Charles Oliver Nutter
Martin DeMello
Ara.T.Howard
Fedzor
|