go-zookeeper / zk

Native ZooKeeper client for Go
BSD 3-Clause "New" or "Revised" License
512 stars 130 forks source link

Optimize comparison #55

Closed moredure closed 3 years ago

moredure commented 3 years ago
func BenchmarkCompare(b *testing.B) {
    var response = []byte("imokres")
    b.Run("1", func(b *testing.B) {
        b.ReportAllocs()
        for i := 0; i < b.N; i++ {
            if bytes.Equal(response[:4], []byte("imok")) {

            }
        }
    })
    b.Run("2", func(b *testing.B) {
        b.ReportAllocs()
        for i := 0; i < b.N; i++ {
            if string(response[:4]) == "imok" {
            }
        }
    })
}
cpu: Intel(R) Core(TM) i5-7600 CPU @ 3.50GHz
BenchmarkCompare
BenchmarkCompare/1
BenchmarkCompare/1-4    195669936            5.736 ns/op           0 B/op          0 allocs/op
BenchmarkCompare/2
BenchmarkCompare/2-4    1000000000           0.2531 ns/op          0 B/op          0 allocs/op
PASS
codecov[bot] commented 3 years ago

Codecov Report

Merging #55 (6d7c3ff) into master (54f4812) will increase coverage by 0.33%. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #55      +/-   ##
==========================================
+ Coverage   76.11%   76.45%   +0.33%     
==========================================
  Files           7        7              
  Lines        1189     1189              
==========================================
+ Hits          905      909       +4     
+ Misses        195      193       -2     
+ Partials       89       87       -2     
Impacted Files Coverage Δ
flw.go 82.66% <100.00%> (ø)
conn.go 73.78% <0.00%> (+0.62%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 54f4812...6d7c3ff. Read the comment docs.

moredure commented 3 years ago

@pmazzini, you're welcome!