github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/util/memset_test.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package util 22 23 import ( 24 "testing" 25 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func setValues(values []float64, initialValue float64) { 30 for i := 0; i < len(values); i++ { 31 values[i] = initialValue 32 } 33 } 34 func BenchmarkMemsetZeroValues(b *testing.B) { 35 values := make([]float64, 10000) 36 for i := 0; i < b.N; i++ { 37 Memset(values, 0) 38 } 39 } 40 41 func BenchmarkLoopZeroValues(b *testing.B) { 42 values := make([]float64, 10000) 43 for i := 0; i < b.N; i++ { 44 setValues(values, 0) 45 } 46 } 47 48 func BenchmarkMemsetNonZeroValues(b *testing.B) { 49 values := make([]float64, 10000) 50 for i := 0; i < b.N; i++ { 51 Memset(values, 1) 52 } 53 54 } 55 56 func BenchmarkLoopNonZeroValues(b *testing.B) { 57 values := make([]float64, 10000) 58 for i := 0; i < b.N; i++ { 59 setValues(values, 1) 60 } 61 } 62 63 func TestMemsetIntValues(t *testing.T) { 64 values := make([]int, 10000) 65 MemsetInt(values, 8) 66 for _, v := range values { 67 assert.Equal(t, 8, v) 68 } 69 } 70 71 func setIntValues(values []int, initialValue int) { 72 for i := 0; i < len(values); i++ { 73 values[i] = initialValue 74 } 75 } 76 77 func BenchmarkMemsetIntZeroValues(b *testing.B) { 78 values := make([]int, 10000) 79 for i := 0; i < b.N; i++ { 80 MemsetInt(values, 0) 81 } 82 } 83 84 func BenchmarkLoopIntZeroValues(b *testing.B) { 85 values := make([]int, 10000) 86 for i := 0; i < b.N; i++ { 87 setIntValues(values, 0) 88 } 89 } 90 91 func BenchmarkMemsetIntNonZeroValues(b *testing.B) { 92 values := make([]int, 10000) 93 for i := 0; i < b.N; i++ { 94 MemsetInt(values, 1) 95 } 96 } 97 98 func BenchmarkLoopIntNonZeroValues(b *testing.B) { 99 values := make([]int, 10000) 100 for i := 0; i < b.N; i++ { 101 setIntValues(values, 1) 102 } 103 }