Go — A Benchmark To Compare Synchronization Techniques

Go — A Benchmark To Compare Synchronization TechniquesGolang synchronizes multiple goroutines via channels and sync packages to prevent multiple goroutines from competing for shared data.Photo by James Harrison on UnsplashIn Golang, you can avoid concu…


This content originally appeared on Level Up Coding - Medium and was authored by Radhakishan Surwase

Go — A Benchmark To Compare Synchronization Techniques

Golang synchronizes multiple goroutines via channels and sync packages to prevent multiple goroutines from competing for shared data.

Photo by James Harrison on Unsplash

In Golang, you can avoid concurrent modification of a global variable by using the below-mentioned synchronization techniques.

  1. Using sync/atomic
  2. Using sync.Mutex
  3. Using channel

In this article, we study how to do it and their benchmarking result as well.

Benchmark without synchronization technique.

Code Listing -1

A global variable g is modified in Benchmark and no synchronization technique is applied. Then basic benchmarking results for the above code is as below

$ go test -bench=. -benchtime=5s
goos: windows
goarch: amd64
pkg: sync-benchmark
cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
Benchmark_NoSync-8 1000000000 1.802 ns/op
PASS
ok sync-benchmark 3.113s

Benchmark with sync/atomic

Code Listing -2

A global variable g is modified in Benchmark and this time sync/atomic synchronization technique is used. Then basic benchmarking results for the above code is as below

$ go test -bench=. -benchtime=5s
goos: windows
goarch: amd64
pkg: sync-benchmark
cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
Benchmark_Atomic-8 1000000000 5.237 ns/op
PASS
ok sync-benchmark 6.880s

Benchmark with sync. Mutex

Code Listing -3

A global variable g is modified in Benchmark and this time sync. Mutex synchronization technique is used. Then basic benchmarking results for the above code is as below

$ go test -bench=. -benchtime=5s
goos: windows
goarch: amd64
pkg: sync-benchmark
cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
Benchmark_Mutex-8 526245530 10.79 ns/op
PASS
ok sync-benchmark 7.885s

Benchmark with using the channel.

Code Listing -4

A global variable g is modified in Benchmark and this time channels are used for synchronization. Then basic benchmarking results for the above code is as below

$ go test -bench=. -benchtime=5s
goos: windows
goarch: amd64
pkg: sync-benchmark
cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
Benchmark_Channel-8 168880364 35.91 ns/op
PASS
ok sync-benchmark 10.809s

Benchmark Comparison

This time we have tried to run all types of benchmarks & we observe the following result.

$ go test -bench=. -benchtime=5s
goos: windows
goarch: amd64
pkg: sync-benchmark
cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
Benchmark_NoSync-8       1000000000               1.695 ns/op
Benchmark_Atomic-8 1000000000 5.232 ns/op
Benchmark_Mutex-8 558550783 10.86 ns/op
Benchmark_Channel-8 168589561 35.56 ns/op
PASS
ok sync-benchmark 25.421s

Click here to see the code on the playground. You can copy it on your local and run it.

Conclusion

From the results, we could identify that using channels to concurrently increase a value is much slower than the other techniques. The atomic way is the best way.

If it is possible, we should try to not share a value between multiple goroutines, so that we don’t need to do synchronizations at all for the value.

That’s all for now. Happy learning. 😍


Go — A Benchmark To Compare Synchronization Techniques was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Radhakishan Surwase


Print Share Comment Cite Upload Translate Updates
APA

Radhakishan Surwase | Sciencx (2022-03-13T01:59:25+00:00) Go — A Benchmark To Compare Synchronization Techniques. Retrieved from https://www.scien.cx/2022/03/13/go-a-benchmark-to-compare-synchronization-techniques/

MLA
" » Go — A Benchmark To Compare Synchronization Techniques." Radhakishan Surwase | Sciencx - Sunday March 13, 2022, https://www.scien.cx/2022/03/13/go-a-benchmark-to-compare-synchronization-techniques/
HARVARD
Radhakishan Surwase | Sciencx Sunday March 13, 2022 » Go — A Benchmark To Compare Synchronization Techniques., viewed ,<https://www.scien.cx/2022/03/13/go-a-benchmark-to-compare-synchronization-techniques/>
VANCOUVER
Radhakishan Surwase | Sciencx - » Go — A Benchmark To Compare Synchronization Techniques. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/13/go-a-benchmark-to-compare-synchronization-techniques/
CHICAGO
" » Go — A Benchmark To Compare Synchronization Techniques." Radhakishan Surwase | Sciencx - Accessed . https://www.scien.cx/2022/03/13/go-a-benchmark-to-compare-synchronization-techniques/
IEEE
" » Go — A Benchmark To Compare Synchronization Techniques." Radhakishan Surwase | Sciencx [Online]. Available: https://www.scien.cx/2022/03/13/go-a-benchmark-to-compare-synchronization-techniques/. [Accessed: ]
rf:citation
» Go — A Benchmark To Compare Synchronization Techniques | Radhakishan Surwase | Sciencx | https://www.scien.cx/2022/03/13/go-a-benchmark-to-compare-synchronization-techniques/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.