Go Synchronization Methods Benchmark
A benchmark to compare the performance of channel and mutex synchronization in Go.
Description:This benchmark compares the performance of channel and mutex synchronization in Go.
Implementations
Green marks the fastest implementation, red marks the slowest
Comparison
This section compares the performance of Synchronization Methods
implementations with the
functions: run
Different Run Count
Different CPU Core Count
Channel
This benchmark uses a channel to synchronize the goroutines. The channel is used as a lock, so only one goroutine can access the counter at the same time.
Function | ns/op | ops/sec | B/op | allocs/op | MB/s |
---|---|---|---|---|---|
run | 999.83 | 1225486 | 156.7 | 1 | 0.0 |
Comparison
- Channel
run
is 176.94% slower than Mutexrun
.
func BenchmarkChannel_run(b *testing.B) {
var counter int
channel := make(chan bool, 1) // Buffered channel with capacity of 1
b.ResetTimer()
var wg sync.WaitGroup
for i := 0; i < b.N; i++ {
wg.Add(1)
go func() {
channel <- true // Acquire the "lock"
counter++
<-channel // Release the "lock"
wg.Done()
}()
}
wg.Wait()
close(channel)
}
Channel run
ns/op | ops/sec | B/op | allocs/op | MB/s |
---|---|---|---|---|
999.83 | 1225486 | 156.7 | 1 | 0.0 |
By Run Count
By CPU Core Count
Mutex
This benchmark uses a sync.Mutex
to lock the counter, before accessing it. This makes sure, that only one goroutine can access the counter at the same time.
Function | ns/op | ops/sec | B/op | allocs/op | MB/s |
---|---|---|---|---|---|
run | 361.03 | 3338749 | 32.1 | 1 | 0.0 |
Comparison
- Mutex
run
is 63.89% faster than Channelrun
.
func BenchmarkMutex_run(b *testing.B) {
var counter int
var mutex sync.Mutex
b.ResetTimer()
var wg sync.WaitGroup
for i := 0; i < b.N; i++ {
wg.Add(1)
go func() {
mutex.Lock()
counter++
mutex.Unlock()
wg.Done()
}()
}
wg.Wait()
}
Mutex run
ns/op | ops/sec | B/op | allocs/op | MB/s |
---|---|---|---|---|
361.03 | 3338749 | 32.1 | 1 | 0.0 |
By Run Count
By CPU Core Count
Full Benchmark Code
func BenchmarkChannel_run(b *testing.B) {
var counter int
channel := make(chan bool, 1) // Buffered channel with capacity of 1
b.ResetTimer()
var wg sync.WaitGroup
for i := 0; i < b.N; i++ {
wg.Add(1)
go func() {
channel <- true // Acquire the "lock"
counter++
<-channel // Release the "lock"
wg.Done()
}()
}
wg.Wait()
close(channel)
}
func BenchmarkMutex_run(b *testing.B) {
var counter int
var mutex sync.Mutex
b.ResetTimer()
var wg sync.WaitGroup
for i := 0; i < b.N; i++ {
wg.Add(1)
go func() {
mutex.Lock()
counter++
mutex.Unlock()
wg.Done()
}()
}
wg.Wait()
}
Full Benchmark Output
Implementation | Function | Runs | CPU Core Count | ns/op | ops/sec | B/op | allocs/op | MB/s |
---|---|---|---|---|---|---|---|---|
Channel | run | 1000 | 8 | 1020 | 980392.1568627451 | 98 | 1 | 0 |
Channel | run | 2000 | 8 | 956.2 | 1045806.3166701526 | 106 | 1 | 0 |
Channel | run | 3000 | 8 | 962.5 | 1038961.0389610389 | 105 | 1 | 0 |
Channel | run | 4000 | 8 | 993.5 | 1006542.5264217413 | 89 | 1 | 0 |
Channel | run | 5000 | 8 | 985.7 | 1014507.4566298062 | 98 | 1 | 0 |
Channel | run | 6000 | 8 | 915.3 | 1092537.9656943078 | 110 | 1 | 0 |
Channel | run | 7000 | 8 | 1024 | 976562.5 | 106 | 1 | 0 |
Channel | run | 8000 | 8 | 912 | 1096491.2280701755 | 100 | 1 | 0 |
Channel | run | 9000 | 8 | 936.3 | 1068033.7498664958 | 108 | 1 | 0 |
Channel | run | 10000 | 8 | 959.9 | 1041775.1849150953 | 100 | 1 | 0 |
Channel | run | 1000 | 1 | 530 | 1886792.4528301887 | 471 | 2 | 0 |
Channel | run | 2000 | 1 | 677.2 | 1476668.6355581807 | 477 | 2 | 0 |
Channel | run | 3000 | 1 | 649.5 | 1539645.881447267 | 476 | 2 | 0 |
Channel | run | 4000 | 1 | 659.7 | 1515840.5335758678 | 479 | 2 | 0 |
Channel | run | 5000 | 1 | 711.4 | 1405678.942929435 | 473 | 2 | 0 |
Channel | run | 6000 | 1 | 683.9 | 1462202.0763269484 | 478 | 2 | 0 |
Channel | run | 7000 | 1 | 1501 | 666222.5183211192 | 474 | 2 | 0 |
Channel | run | 8000 | 1 | 1297 | 771010.0231303007 | 480 | 2 | 0 |
Channel | run | 9000 | 1 | 1138 | 878734.6221441125 | 476 | 2 | 0 |
Channel | run | 10000 | 1 | 1139 | 877963.125548727 | 483 | 2 | 0 |
Channel | run | 1000 | 2 | 439.5 | 2275312.8555176337 | 67 | 1 | 0 |
Channel | run | 2000 | 2 | 624.4 | 1601537.475976938 | 71 | 1 | 0 |
Channel | run | 3000 | 2 | 790.6 | 1264862.1300278269 | 47 | 1 | 0 |
Channel | run | 4000 | 2 | 525.3 | 1903674.0909956216 | 54 | 1 | 0 |
Channel | run | 5000 | 2 | 609.5 | 1640689.0894175554 | 55 | 1 | 0 |
Channel | run | 6000 | 2 | 618.8 | 1616031.027795734 | 42 | 1 | 0 |
Channel | run | 7000 | 2 | 553.5 | 1806684.7335140018 | 35 | 1 | 0 |
Channel | run | 8000 | 2 | 362.2 | 2760905.5770292655 | 42 | 1 | 0 |
Channel | run | 9000 | 2 | 249.3 | 4011231.4480545525 | 32 | 1 | 0 |
Channel | run | 10000 | 2 | 428.6 | 2333177.788147457 | 45 | 1 | 0 |
Channel | run | 1000 | 4 | 720.6 | 1387732.4451845684 | 77 | 1 | 0 |
Channel | run | 2000 | 4 | 461.3 | 2167786.6897897245 | 66 | 1 | 0 |
Channel | run | 3000 | 4 | 624.2 | 1602050.6247997435 | 82 | 1 | 0 |
Channel | run | 4000 | 4 | 740.9 | 1349709.812390336 | 86 | 1 | 0 |
Channel | run | 5000 | 4 | 740.1 | 1351168.760978246 | 79 | 1 | 0 |
Channel | run | 6000 | 4 | 651.9 | 1533977.6039269827 | 91 | 1 | 0 |
Channel | run | 7000 | 4 | 656.5 | 1523229.2460015232 | 59 | 1 | 0 |
Channel | run | 8000 | 4 | 775.3 | 1289823.2942086936 | 82 | 1 | 0 |
Channel | run | 9000 | 4 | 716.5 | 1395673.4124214933 | 92 | 1 | 0 |
Channel | run | 10000 | 4 | 588.4 | 1699524.133242692 | 74 | 1 | 0 |
Channel | run | 1000 | 16 | 890.4 | 1123090.7457322553 | 94 | 1 | 0 |
Channel | run | 2000 | 16 | 1181 | 846740.050804403 | 114 | 1 | 0 |
Channel | run | 3000 | 16 | 1168 | 856164.3835616439 | 109 | 1 | 0 |
Channel | run | 4000 | 16 | 1233 | 811030.0081103 | 109 | 1 | 0 |
Channel | run | 5000 | 16 | 1230 | 813008.1300813009 | 114 | 1 | 0 |
Channel | run | 6000 | 16 | 1275 | 784313.725490196 | 118 | 1 | 0 |
Channel | run | 7000 | 16 | 1340 | 746268.6567164179 | 120 | 1 | 0 |
Channel | run | 8000 | 16 | 1332 | 750750.7507507508 | 117 | 1 | 0 |
Channel | run | 9000 | 16 | 1305 | 766283.5249042145 | 118 | 1 | 0 |
Channel | run | 10000 | 16 | 1255 | 796812.749003984 | 106 | 1 | 0 |
Channel | run | 1000 | 32 | 1259 | 794281.1755361398 | 114 | 1 | 0 |
Channel | run | 2000 | 32 | 1629 | 613873.5420503377 | 113 | 1 | 0 |
Channel | run | 3000 | 32 | 1620 | 617283.9506172839 | 126 | 1 | 0 |
Channel | run | 4000 | 32 | 1766 | 566251.415628539 | 128 | 1 | 0 |
Channel | run | 5000 | 32 | 1814 | 551267.9162072768 | 126 | 1 | 0 |
Channel | run | 6000 | 32 | 1698 | 588928.1507656066 | 124 | 1 | 0 |
Channel | run | 7000 | 32 | 1912 | 523012.55230125523 | 125 | 1 | 0 |
Channel | run | 8000 | 32 | 1774 | 563697.8579481398 | 125 | 1 | 0 |
Channel | run | 9000 | 32 | 1849 | 540832.8826392645 | 126 | 1 | 0 |
Channel | run | 10000 | 32 | 1930 | 518134.71502590674 | 111 | 1 | 0 |
Mutex | run | 1000 | 1 | 655.9 | 1524622.6558926667 | 32 | 1 | 0 |
Mutex | run | 2000 | 1 | 894.4 | 1118067.9785330947 | 32 | 1 | 0 |
Mutex | run | 3000 | 1 | 699.6 | 1429388.221841052 | 32 | 1 | 0 |
Mutex | run | 4000 | 1 | 775 | 1290322.5806451612 | 32 | 1 | 0 |
Mutex | run | 5000 | 1 | 861.6 | 1160631.383472609 | 32 | 1 | 0 |
Mutex | run | 6000 | 1 | 518.4 | 1929012.3456790124 | 32 | 1 | 0 |
Mutex | run | 7000 | 1 | 685.5 | 1458789.2049598834 | 32 | 1 | 0 |
Mutex | run | 8000 | 1 | 1059 | 944287.0632672332 | 32 | 1 | 0 |
Mutex | run | 9000 | 1 | 685.1 | 1459640.9283316303 | 32 | 1 | 0 |
Mutex | run | 10000 | 1 | 756.1 | 1322576.378785875 | 32 | 1 | 0 |
Mutex | run | 1000 | 32 | 355.3 | 2814522.9383619474 | 32 | 1 | 0 |
Mutex | run | 2000 | 32 | 327.5 | 3053435.1145038167 | 32 | 1 | 0 |
Mutex | run | 3000 | 32 | 331.4 | 3017501.5087507544 | 32 | 1 | 0 |
Mutex | run | 4000 | 32 | 217.4 | 4599816.007359706 | 32 | 1 | 0 |
Mutex | run | 5000 | 32 | 238.1 | 4199916.001679966 | 32 | 1 | 0 |
Mutex | run | 6000 | 32 | 340.1 | 2940311.673037342 | 32 | 1 | 0 |
Mutex | run | 7000 | 32 | 331.3 | 3018412.315122246 | 32 | 1 | 0 |
Mutex | run | 8000 | 32 | 210.5 | 4750593.824228029 | 32 | 1 | 0 |
Mutex | run | 9000 | 32 | 300.8 | 3324468.085106383 | 32 | 1 | 0 |
Mutex | run | 10000 | 32 | 314.5 | 3179650.238473768 | 32 | 1 | 0 |
Mutex | run | 1000 | 2 | 307.2 | 3255208.3333333335 | 32 | 1 | 0 |
Mutex | run | 2000 | 2 | 271.4 | 3684598.3787767137 | 32 | 1 | 0 |
Mutex | run | 3000 | 2 | 268.3 | 3727171.0771524413 | 32 | 1 | 0 |
Mutex | run | 4000 | 2 | 196.7 | 5083884.087442807 | 32 | 1 | 0 |
Mutex | run | 5000 | 2 | 235.9 | 4239084.35777872 | 32 | 1 | 0 |
Mutex | run | 6000 | 2 | 278 | 3597122.3021582733 | 32 | 1 | 0 |
Mutex | run | 7000 | 2 | 578.3 | 1729206.2943109113 | 32 | 1 | 0 |
Mutex | run | 8000 | 2 | 201.5 | 4962779.156327544 | 32 | 1 | 0 |
Mutex | run | 9000 | 2 | 207.4 | 4821600.771456123 | 32 | 1 | 0 |
Mutex | run | 10000 | 2 | 217.3 | 4601932.811780948 | 32 | 1 | 0 |
Mutex | run | 1000 | 4 | 266.7 | 3749531.308586427 | 32 | 1 | 0 |
Mutex | run | 2000 | 4 | 255.9 | 3907776.475185619 | 32 | 1 | 0 |
Mutex | run | 3000 | 4 | 328.1 | 3047851.2648582747 | 32 | 1 | 0 |
Mutex | run | 4000 | 4 | 257.4 | 3885003.8850038853 | 32 | 1 | 0 |
Mutex | run | 5000 | 4 | 291.4 | 3431708.9910775567 | 32 | 1 | 0 |
Mutex | run | 6000 | 4 | 195.1 | 5125576.627370579 | 32 | 1 | 0 |
Mutex | run | 7000 | 4 | 278.9 | 3585514.521333812 | 32 | 1 | 0 |
Mutex | run | 8000 | 4 | 262.1 | 3815337.6573826782 | 32 | 1 | 0 |
Mutex | run | 9000 | 4 | 311 | 3215434.083601286 | 32 | 1 | 0 |
Mutex | run | 10000 | 4 | 263.3 | 3797949.1074819597 | 32 | 1 | 0 |
Mutex | run | 1000 | 8 | 228.3 | 4380201.489268506 | 32 | 1 | 0 |
Mutex | run | 2000 | 8 | 267.3 | 3741114.852225963 | 32 | 1 | 0 |
Mutex | run | 3000 | 8 | 345.8 | 2891844.997108155 | 32 | 1 | 0 |
Mutex | run | 4000 | 8 | 510.2 | 1960015.680125441 | 36 | 1 | 0 |
Mutex | run | 5000 | 8 | 269.8 | 3706449.2216456635 | 32 | 1 | 0 |
Mutex | run | 6000 | 8 | 209.6 | 4770992.366412214 | 32 | 1 | 0 |
Mutex | run | 7000 | 8 | 271.3 | 3685956.5057132323 | 32 | 1 | 0 |
Mutex | run | 8000 | 8 | 210.3 | 4755111.74512601 | 32 | 1 | 0 |
Mutex | run | 9000 | 8 | 257.3 | 3886513.79712398 | 32 | 1 | 0 |
Mutex | run | 10000 | 8 | 270.7 | 3694126.3391207983 | 32 | 1 | 0 |
Mutex | run | 1000 | 16 | 239.4 | 4177109.4402673347 | 32 | 1 | 0 |
Mutex | run | 2000 | 16 | 277.5 | 3603603.603603604 | 32 | 1 | 0 |
Mutex | run | 3000 | 16 | 304.4 | 3285151.11695138 | 32 | 1 | 0 |
Mutex | run | 4000 | 16 | 217.2 | 4604051.5653775325 | 32 | 1 | 0 |
Mutex | run | 5000 | 16 | 282.1 | 3544842.2545196735 | 32 | 1 | 0 |
Mutex | run | 6000 | 16 | 296.9 | 3368137.4200067366 | 32 | 1 | 0 |
Mutex | run | 7000 | 16 | 403.3 | 2479543.763947434 | 32 | 1 | 0 |
Mutex | run | 8000 | 16 | 201.6 | 4960317.460317461 | 32 | 1 | 0 |
Mutex | run | 9000 | 16 | 276.5 | 3616636.528028933 | 32 | 1 | 0 |
Mutex | run | 10000 | 16 | 293 | 3412969.2832764504 | 32 | 1 | 0 |