File tree Expand file tree Collapse file tree 10 files changed +171
-0
lines changed
Expand file tree Collapse file tree 10 files changed +171
-0
lines changed Original file line number Diff line number Diff line change 1+ .DS_Store
2+ bin
Original file line number Diff line number Diff line change 1+ # Concurrency
2+
3+ This tests compares Rust and Go on concurrent code.
4+
5+ # Run Test
6+ ```
7+ ./build-and-run.sh
8+ ```
9+
10+ 1 . hello1 creates 10 concurrent rountines and print hello to the stdout
11+ 2 . hello2 creates 1000 concurrent routines and print hello to the stdout
12+
13+ # Testing Result
14+ | # | Go | Rust | Ruby |
15+ | ---| ---| ---| ---|
16+ | 10 | 0.23s | 0.14s | 0.170s |
17+ | 1000 | 0.21s | 0.97s | 14.895s |
18+
19+ * Testing platform: Macbook Air (Late 2010). Processor: 2.13 GHz Intel Core 2 Duo, Memory: 4 GB 1067 MHz DDR3
20+
21+ # Conclusion
22+
23+ The result clearly shows that Go's ` goroutine ` is more lightweight and faster than Rust's ` spawn ` or Ruby's Thread.
Original file line number Diff line number Diff line change 1+ #! /bin/sh -e
2+
3+ # Build
4+ mkdir -p bin
5+ go build -o bin/hello1_go go/hello1/main.go
6+ go build -o bin/hello2_go go/hello2/main.go
7+ rustc -o bin/hello1_rust rust/hello1.rs
8+ rustc -o bin/hello2_rust rust/hello2.rs
9+
10+ # Run
11+
12+ echo
13+ echo " ## Go"
14+ echo " ### hello1:"
15+ time bin/hello1_go > /tmp/hello1_go.out
16+
17+ echo
18+ echo " ### hello2:"
19+ time bin/hello2_go > /tmp/hello2_go.out
20+
21+ echo
22+ echo " ## Rust"
23+ echo " ### hello1"
24+ time bin/hello1_rust > /tmp/hello1_rust.out
25+
26+ echo
27+ echo " ### hello2"
28+ time bin/hello2_rust > /tmp/hello2_rust.out
29+
30+ echo
31+ echo " ## Ruby"
32+ echo " ### hello1"
33+ time ruby ruby/hello1.rb > /tmp/hello1_ruby.out
34+
35+ echo
36+ echo " ### hello2"
37+ time ruby ruby/hello2.rb > /tmp/hello2_ruby.out
38+
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import "fmt"
4+
5+ func main () {
6+ quitSignal := make (chan int )
7+
8+ greetings := "hello"
9+ for i := 0 ; i < 1000 ; i ++ {
10+ go func (i int , signal chan int ) {
11+ fmt .Printf ("%s from goroutine number %d\n " , greetings , i )
12+ signal <- 1
13+ }(i , quitSignal )
14+ }
15+
16+ // make the program don't exit until all
17+ // 10 goroutines are finished
18+ for i := 0 ; i < 1000 ; i ++ {
19+ <- quitSignal
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import "fmt"
4+
5+ func main () {
6+ quitSignal := make (chan int )
7+
8+ greetings := "hello"
9+ for i := 0 ; i < 1000 ; i ++ {
10+ go func (i int , signal chan int ) {
11+ fmt .Printf ("%s from goroutine number %d\n " , greetings , i )
12+ signal <- 1
13+ }(i , quitSignal )
14+ }
15+
16+ // make the program don't exit until all
17+ // 10 goroutines are finished
18+ for i := 0 ; i < 1000 ; i ++ {
19+ <- quitSignal
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ count = 0
2+
3+ 10 . times do |i |
4+ Thread . new do
5+ greeting_message = "Hello"
6+
7+ # This is weird in Ruby but it's closer to the println! macro
8+ # usage in the Rust example.
9+ puts "#{ greeting_message } from lightweight thread number #{ i } \n "
10+
11+ count = count + 1
12+ end
13+ end
14+
15+ # wait for all thread finishes
16+ while count < 10 do
17+ # do nothing
18+ end
Original file line number Diff line number Diff line change 1+ count = 0
2+
3+ 1000 . times do |i |
4+ Thread . new do
5+ greeting_message = "Hello"
6+
7+ # This is weird in Ruby but it's closer to the println! macro
8+ # usage in the Rust example.
9+ puts "#{ greeting_message } from lightweight thread number #{ i } \n "
10+
11+ count = count + 1
12+ end
13+ end
14+
15+ # wait for all thread finishes
16+ while count < 1000 do
17+ # do nothing
18+ end
Original file line number Diff line number Diff line change 1+ /* This function creates ten "tasks" that all execute concurrently.
2+ To verify this, run the program several times and observe the irregular
3+ order in which each task's output is printed. */
4+ fn main ( ) {
5+ // This string is immutable, so it can safely be accessed from multiple tasks.
6+ let greeting = "Hello" ;
7+ // `for` loops work with any type that implements the `Iterator` trait.
8+ for num in range ( 0 i, 10 ) {
9+ spawn ( proc ( ) {
10+ // `println!` is a macro that statically typechecks a format string.
11+ // Macros are structural (as in Scheme) rather than textual (as in C).
12+ println ! ( "{:s} from lightweight thread number {:i}" , greeting, num) ;
13+ } ) ;
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ /* This function creates ten "tasks" that all execute concurrently.
2+ To verify this, run the program several times and observe the irregular
3+ order in which each task's output is printed. */
4+ fn main ( ) {
5+ // This string is immutable, so it can safely be accessed from multiple tasks.
6+ let greeting = "Hello" ;
7+ // `for` loops work with any type that implements the `Iterator` trait.
8+ for num in range ( 0 i, 1000 ) {
9+ spawn ( proc ( ) {
10+ // `println!` is a macro that statically typechecks a format string.
11+ // Macros are structural (as in Scheme) rather than textual (as in C).
12+ println ! ( "{:s} from lightweight thread number {:i}" , greeting, num) ;
13+ } ) ;
14+ }
15+ }
You can’t perform that action at this time.
0 commit comments