Skip to content

Commit 9713861

Browse files
committed
initial commit
0 parents  commit 9713861

File tree

10 files changed

+171
-0
lines changed

10 files changed

+171
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
bin

concurrency/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.

concurrency/build_and_run.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+

concurrency/go/hello1/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

concurrency/go/hello2/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

concurrency/go/main

1.73 MB
Binary file not shown.

concurrency/ruby/hello1.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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

concurrency/ruby/hello2.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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

concurrency/rust/hello1.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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(0i, 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+
}

concurrency/rust/hello2.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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(0i, 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+
}

0 commit comments

Comments
 (0)