Last active
October 16, 2017 00:35
-
-
Save masutaka/c0a4234a5264c89655c40adcf7c27cb2 to your computer and use it in GitHub Desktop.
Serial and parallel processings with ruby and golang
This file contains 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
// parallels processing v1 (broken code) | |
package main | |
import ( | |
"fmt" | |
"runtime" | |
"time" | |
) | |
func heavyProcess(i int) { | |
sleepSecond := 3 * time.Second | |
fmt.Printf("i: %2d, sleep: %v, goroutineNum: %d\n", i, sleepSecond, runtime.NumGoroutine()) | |
time.Sleep(sleepSecond) | |
} | |
func main() { | |
for i := 0; i < 30; i++ { | |
go heavyProcess(i) | |
} | |
} |
This file contains 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
#!/usr/bin/env ruby | |
# parallels processing v1 | |
def thread_num | |
Thread.list.select {|thread| thread.status == 'run'}.count | |
end | |
def heavy_process(i) | |
sleepSecond = 3 | |
print "i: %2d, sleep: %ds, thread_num: %d\n" % [i, sleepSecond, thread_num] | |
sleep(sleepSecond) | |
end | |
# 5 parallels | |
thread_num = 5 | |
threads = [] | |
results = [] | |
mutex = Mutex::new | |
(30 / thread_num).times do |a| | |
thread_num.times do |b| | |
threads << Thread.start do | |
i = (a * thread_num) + b | |
heavy_process(i) | |
mutex.synchronize { results << i } | |
end | |
end | |
threads.each(&:join) | |
end | |
# Print saved results | |
print "------------\n" | |
printf "result.count: %d\n", results.count | |
results.each do |v| | |
printf "i: %d\n", v | |
end |
This file contains 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
// parallels processing v2 (broken code) | |
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func heavyProcess(i int) { | |
sleepSecond := 3 * time.Second | |
fmt.Printf("i: %2d, sleep: %v, goroutineNum: %d\n", i, sleepSecond, runtime.NumGoroutine()) | |
time.Sleep(sleepSecond) | |
} | |
func main() { | |
var wg sync.WaitGroup | |
for i := 0; i < 30; i++ { | |
wg.Add(1) | |
go heavyProcess(i) | |
wg.Done() | |
} | |
wg.Wait() | |
} |
This file contains 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
#!/usr/bin/env ruby | |
# parallels processing v2 using third party gem | |
require 'parallel' | |
def thread_num | |
Thread.list.select {|thread| thread.status == 'run'}.count | |
end | |
def heavy_process(i) | |
sleepSecond = 3 | |
print "i: %2d, sleep: %ds, thread_num: %d\n" % [i, sleepSecond, thread_num] | |
sleep(sleepSecond) | |
end | |
# 5 parallels | |
thread_num = 5 | |
results = [] | |
mutex = Mutex::new | |
Parallel.each(0..29, in_threads: thread_num) do |i| | |
heavy_process(i) | |
mutex.synchronize { results << i } | |
end | |
# Print saved results | |
print "------------\n" | |
printf "result.count: %d\n", results.count | |
results.each do |v| | |
printf "i: %d\n", v | |
end |
This file contains 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
// parallels processing v3 | |
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func heavyProcess(i int) { | |
sleepSecond := 3 * time.Second | |
fmt.Printf("i: %2d, sleep: %v, goroutineNum: %d\n", i, sleepSecond, runtime.NumGoroutine()) | |
time.Sleep(sleepSecond) | |
} | |
func main() { | |
var wg sync.WaitGroup | |
for i := 0; i < 30; i++ { | |
wg.Add(1) | |
go func(i int) { | |
heavyProcess(i) | |
wg.Done() | |
}(i) | |
} | |
wg.Wait() | |
} |
This file contains 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
// parallels processing v4 | |
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func heavyProcess(i int) { | |
sleepSecond := 3 * time.Second | |
fmt.Printf("i: %2d, sleep: %v, goroutineNum: %d\n", i, sleepSecond, runtime.NumGoroutine()) | |
time.Sleep(sleepSecond) | |
} | |
func main() { | |
var wg sync.WaitGroup | |
for i := 0; i < 30; i++ { | |
wg.Add(1) | |
go func(i int) { | |
defer wg.Done() | |
heavyProcess(i) | |
}(i) | |
} | |
wg.Wait() | |
} |
This file contains 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
// parallels processing v5 | |
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func heavyProcess(i int) { | |
sleepSecond := 3 * time.Second | |
fmt.Printf("i: %2d, sleep: %v, goroutineNum: %d\n", i, sleepSecond, runtime.NumGoroutine()) | |
time.Sleep(sleepSecond) | |
} | |
func main() { | |
var wg sync.WaitGroup | |
// 5 parallels | |
semaphore := make(chan int, 5) | |
for i := 0; i < 30; i++ { | |
wg.Add(1) | |
go func(i int) { | |
defer wg.Done() | |
semaphore <- 1 | |
heavyProcess(i) | |
<-semaphore | |
}(i) | |
} | |
wg.Wait() | |
} |
This file contains 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
// parallels processing v5b | |
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func heavyProcess(i int) { | |
sleepSecond := 3 * time.Second | |
fmt.Printf("i: %2d, sleep: %v, goroutineNum: %d\n", i, sleepSecond, runtime.NumGoroutine()) | |
time.Sleep(sleepSecond) | |
} | |
func main() { | |
var wg sync.WaitGroup | |
cpuNum := runtime.NumCPU() | |
fmt.Printf("cpuNum: %d\n", cpuNum) | |
// CPU num parallels | |
semaphore := make(chan int, cpuNum) | |
for i := 0; i < 30; i++ { | |
wg.Add(1) | |
go func(i int) { | |
defer wg.Done() | |
semaphore <- 1 | |
heavyProcess(i) | |
<-semaphore | |
}(i) | |
} | |
wg.Wait() | |
} |
This file contains 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
// parallels processing v6 | |
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func heavyProcess(i int) { | |
sleepSecond := 3 * time.Second | |
fmt.Printf("i: %2d, sleep: %v, goroutineNum: %d\n", i, sleepSecond, runtime.NumGoroutine()) | |
time.Sleep(sleepSecond) | |
} | |
func main() { | |
var wg sync.WaitGroup | |
var results []int | |
var mu sync.Mutex | |
// 5 parallels | |
semaphore := make(chan int, 5) | |
for i := 0; i < 30; i++ { | |
wg.Add(1) | |
go func(i int) { | |
defer wg.Done() | |
semaphore <- 1 | |
heavyProcess(i) | |
<-semaphore | |
mu.Lock() | |
defer mu.Unlock() | |
results = append(results, i) | |
}(i) | |
} | |
wg.Wait() | |
// Print saved results | |
fmt.Print("------------\n") | |
fmt.Printf("len(result): %d\n", len(results)) | |
for _, v := range results { | |
fmt.Printf("i: %d\n", v) | |
} | |
} |
This file contains 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
// parallels processing v7 | |
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func heavyProcess(i int) { | |
sleepSecond := 3 * time.Second | |
fmt.Printf("i: %2d, sleep: %v, goroutineNum: %d\n", i, sleepSecond, runtime.NumGoroutine()) | |
time.Sleep(sleepSecond) | |
} | |
func main() { | |
var wg sync.WaitGroup | |
var results []int | |
var mu sync.Mutex | |
// 5 parallels | |
semaphore := make(chan int, 5) | |
for i := 0; i < 30; i++ { | |
wg.Add(1) | |
go func(i int) { | |
defer wg.Done() | |
semaphore <- i | |
heavyProcess(i) | |
mu.Lock() | |
defer mu.Unlock() | |
results = append(results, <-semaphore) // Is this safe? | |
}(i) | |
} | |
wg.Wait() | |
// Print saved results | |
fmt.Print("------------\n") | |
fmt.Printf("len(result): %d\n", len(results)) | |
for _, v := range results { | |
fmt.Printf("i: %d\n", v) | |
} | |
} |
This file contains 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
// Serial processing | |
package main | |
import ( | |
"fmt" | |
"runtime" | |
"time" | |
) | |
func heavyProcess(i int) { | |
sleepSecond := 3 * time.Second | |
fmt.Printf("i: %2d, sleep: %v, goroutineNum: %d\n", i, sleepSecond, runtime.NumGoroutine()) | |
time.Sleep(sleepSecond) | |
} | |
func main() { | |
for i := 0; i < 30; i++ { | |
heavyProcess(i) | |
} | |
} |
This file contains 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
#!/usr/bin/env ruby | |
# Serial processing | |
def thread_num | |
Thread.list.select {|thread| thread.status == 'run'}.count | |
end | |
def heavy_process(i) | |
sleepSecond = 3 | |
print "i: %2d, sleep: %ds, thread_num: %d\n" % [i, sleepSecond, thread_num] | |
sleep(sleepSecond) | |
end | |
30.times do |i| | |
heavy_process(i) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
memo:
$ GOTRACEBACK=2 go run serial.go