U
U
uvelichitel2018-07-05 00:06:35
go
uvelichitel, 2018-07-05 00:06:35

Give an example of efficient parallel code in Go?

I have written quite a lot of production code in Go. I use Goroutins only for asynchronous servicing of ports, sockets, files. I have never been able to effectively parallelize the computational algorithm, not once. Even the classic sieve of Eratosthenes, authored by the language development team

package main
func Generate(ch chan<- int) {
  for i := 2; ; i++ {
    ch <- i 
  }
}
func Filter(in <-chan int, out chan<- int, prime int) {
  for {
    i := <-in 
    if i%prime != 0 {
      out <- i 
    }
  }
}
func main() {
  ch := make(chan int) 
  go Generate(ch) 
  for i := 0; i < 10; i++ {
    prime := <-ch
    print(prime, "\n")
    ch1 := make(chan int)
    go Filter(ch, ch1, prime)
    ch = ch1
  }
}
on my best machine (two stones 20 cores) does not give any significant increase to the serial version.
What am I missing, please show an efficient parallel code.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
rustler2000, 2018-07-05
@uvelichitel

https://medium.com/@_orcaman/when-too-much-concurr...
https://github.com/klauspost/pgzip/blob/master/REA... see Benchmark
https://github. com/anthonynsimon/bild - well, there are no benchmarks for GOMAXPROCS=1 vs real
+ block encryption of a large amount of data

M
m0nym, 2018-07-12
@m0nym

And parallel code does not always give an advantage. Sometimes the overhead for locks is significantly higher.
Therefore, in particular, many serious applications are single-threaded.
And your test is too small, a lot of overhead, not related to parallelization.

P
Petr Vasiliev, 2018-07-05
@danial72

It seems to me that the efficiency in parallel computing is only for I/O operations, and in other cases it is just fast enough.
The same web server or program that writes to files in a bunch of streams.
But there is little use in the calculations

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question