Go concurrency

Time:2021-5-3

Golang naturally supports concurrency at the language level. It’s a very good language. Sometimes when we encounter complex scenarios in business development, we need to use it for concurrency. We use the coprocessor group to complete the concurrency of multiple requests. When we encounter nested loops, there are context relationships that need to be transformed into concurrent requests. We need to change the previous time complexity from O (n ^ 2) to o (n), Can the time complexity be further reduced to o (1)? On the emergence of nested concurrency. Specific how to nested concurrency, how to write. Today step by step analysis

Serial execution

  1. The time complexity is O (n ^ 2)
  2. No concurrency
  3. Results the execution time was 9s
//Serial execution
func SerializeRun() {
	start := time.Now()
	xx := []int{1, 2, 3}
	yy := []int{100, 200, 300}
	for _, x := range xx {
		for _, y := range yy {
			abc(x, y)
		}
	}
	FMT. Printf ("total time of serial execution: S / N", time. Since (start))
}

func abc(x, y int) {
	time.Sleep(time.Second * 1)
	fmt.Printf("x:%d, y:%d\n", x, y)
}

results of enforcement

x:1, y:100
x:1, y:200
x:1, y:300
x:2, y:100
x:2, y:200
x:2, y:300
x:3, y:100
x:3, y:200
x:3, y:300
Total serial execution time: 9.0026338s

Single coroutine group concurrency

  1. Using the coroutine group to reduce o (n ^ 2) to o (n)
  2. Results the execution time was 3S
//Single parallel execution
func SingleConcurrenceRun() {
	start := time.Now()
	xx := []int{1, 2, 3}
	yy := []int{100, 200, 300}
	for _, x := range xx {
		wgg := sync.WaitGroup{}
		for _, y := range yy {
			wgg.Add(1)
			go func(x, y int) {
				defer wgg.Done()
				abc(x, y)
			}(x, y)
		}
		wgg.Wait()
	}
	FMT. Printf ("total time of single and parallel execution: S / N", time. Since (start))
}
func abc(x, y int) {
	time.Sleep(time.Second * 1)
	fmt.Printf("x:%d, y:%d\n", x, y)
}

result

x:1, y:300
x:1, y:200
x:1, y:100
x:2, y:100
x:2, y:200
x:2, y:300
x:3, y:300
x:3, y:100
x:3, y:200
Total single parallel execution time: 3.0013813s

Nested concurrent execution

  1. Use nested coroutine groups to perform concurrency
  2. Reduce o (n ^ 2) to o (1)
  3. Results the execution time was 1 s
//Nested execution
func NestConcurrenceRun() {
	xx := []int{1, 2, 3}
	yy := []int{100, 200, 300}
	start := time.Now()
	wgg := sync.WaitGroup{}
	for _, x := range xx {
		wgg.Add(1)
		go func(x int) {
			wg := sync.WaitGroup{}
			for _, y := range yy {
				wg.Add(1)
				go func(x, y int) {
					defer wg.Done()
					abc(x, y)
				}(x, y)
			}
			wg.Wait()
			wgg.Done()
		}(x)
	}
	wgg.Wait()
	FMT. Printf ("total time of nested concurrent execution: S / N", time. Since (start))
}
func abc(x, y int) {
	time.Sleep(time.Second * 1)
	fmt.Printf("x:%d, y:%d\n", x, y)
}

result

x:1, y:200
x:3, y:300
x:3, y:200
x:1, y:300
x:2, y:200
x:1, y:100
x:2, y:300
x:2, y:100
x:3, y:100
Total nested concurrent execution time: 1.0023542s

The above is a detailed explanation of go concurrency. For more information about go concurrency, please pay attention to other related articles of developer!

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]