ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
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

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved