Created
March 30, 2011 04:53
-
-
Save donpdonp/893880 to your computer and use it in GitHub Desktop.
parallel ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat parallel.rb | |
# parallel | |
work=["A","B"] | |
# Launch threads | |
work.map! do |job| | |
Thread.new do | |
puts "I got #{job}" | |
20000000.times { 4.0/2.0 } | |
end | |
end | |
# Wait for each thread to finish | |
work.each { |thread| thread.join } | |
$ time /usr/local/ruby-enterprise-1.8.7-2010.01/bin/ruby parallel.rb | |
I got A | |
I got B | |
real 0m9.226s | |
top says RSS 2340k, 1 core | |
$ time /usr/local/ruby/1.9.2-p136/bin/ruby parallel.rb | |
I got BI got A | |
real 0m7.580s | |
top says RSS 2648k, 1 core | |
$ time /usr/local/ruby/rbx-1.2.3/bin/ruby parallel.rb | |
I got A | |
I got B | |
real 0m5.463s | |
top says 26,000k 1 core | |
$ time /usr/local/java/jruby-1.6.0/bin/jruby parallel.rb | |
I got A | |
I got B | |
real 0m7.130s | |
top says 29,000k *2 cores* | |
$ cat parallel.go | |
package main | |
import "fmt" | |
func main() { | |
ch := make(chan string) | |
work := []string{"A","B"} | |
for i := 0; i < len(work); i++ { | |
go func(job string) { | |
fmt.Printf("I got %s\n", job) | |
for i := 0; i < 20000000; i++ { | |
_ = 4.0 / 2.0 | |
} | |
ch <- job | |
}(work[i]) | |
} | |
<- ch | |
<- ch | |
} | |
$ 8c parallel.go | |
$ time ./8.out | |
I got A | |
I got B | |
real 0m0.054s | |
top says RSS 980k 1 core |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment