github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/utils/compareutils_test.go (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package utils 18 19 import ( 20 "bytes" 21 "strings" 22 "testing" 23 "unsafe" 24 ) 25 26 var ( 27 str = strings.Repeat("a", 100) 28 bArr = []byte(strings.Repeat("a", 99) + "b") 29 ) 30 31 func Benchmark_UnsafeEqual(b *testing.B) { 32 b.ResetTimer() 33 b.ReportAllocs() 34 35 for i := 0; i < b.N; i++ { 36 bbp := *(*string)(unsafe.Pointer(&bArr)) 37 _ = str == bbp 38 } 39 } 40 41 func Benchmark_StrEqual(b *testing.B) { 42 b.ResetTimer() 43 b.ReportAllocs() 44 45 for i := 0; i < b.N; i++ { 46 _ = string(bArr) == str 47 } 48 } 49 50 func Benchmark_ByteEqual(b *testing.B) { 51 strBarr := []byte(str) 52 b.ResetTimer() 53 b.ReportAllocs() 54 for i := 0; i < b.N; i++ { 55 _ = bytes.Equal(bArr, strBarr) 56 } 57 }