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.

In Golang, you can avoid concurrent modification of a global variable by using the below-mentioned synchronization techniques.
- Using sync/atomic
- Using sync.Mutex
- Using channel
In this article, we study how to do it and their benchmarking result as well.
Benchmark without synchronization technique.

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

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

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.

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

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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.