Skip to content

Instantly share code, notes, and snippets.

@masutaka
Last active October 16, 2017 00:35
Show Gist options
  • Save masutaka/c0a4234a5264c89655c40adcf7c27cb2 to your computer and use it in GitHub Desktop.
Save masutaka/c0a4234a5264c89655c40adcf7c27cb2 to your computer and use it in GitHub Desktop.
Serial and parallel processings with ruby and golang
// 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)
}
}
#!/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
// 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()
}
#!/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
// 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()
}
// 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()
}
// 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()
}
// 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()
}
// 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)
}
}
// 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)
}
}
// 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)
}
}
#!/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
@masutaka
Copy link
Author

masutaka commented Sep 7, 2017

memo: $ GOTRACEBACK=2 go run serial.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment