github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/bench/audiobuffer/buf_test.go (about) 1 package audiobuffer 2 3 import "testing" 4 5 type Interleaved struct{ Data []float32 } 6 7 func NewInterleaved(frames int) *Interleaved { 8 return &Interleaved{ 9 Data: make([]float32, frames*2), 10 } 11 } 12 13 type Interleaved2 struct{ Data [][2]float32 } 14 15 func NewInterleaved2(frames int) *Interleaved2 { 16 return &Interleaved2{ 17 Data: make([][2]float32, frames), 18 } 19 } 20 21 type Deinterleaved struct{ Data []float32 } 22 23 func NewDeinterleaved(frames int) *Deinterleaved { 24 return &Deinterleaved{ 25 Data: make([]float32, frames*2), 26 } 27 } 28 29 const BufferSize = 64 30 31 func params() (a, b float32) { 32 return 0.5, 0.25 33 } 34 35 func BenchmarkInterleaved(b *testing.B) { 36 buf := NewInterleaved(BufferSize) 37 for i := 0; i < b.N; i++ { 38 a, b := params() 39 for i := 0; i < len(buf.Data); i += 2 { 40 buf.Data[i+0] *= a 41 buf.Data[i+1] *= b 42 a *= 0.99999 43 b *= 0.99999 44 } 45 } 46 } 47 48 func BenchmarkInterleavedVariable(b *testing.B) { 49 buf := NewInterleaved(BufferSize) 50 n := 2 51 for i := 0; i < b.N; i++ { 52 a, b := params() 53 s := []float32{a, b} 54 for i := 0; i < len(buf.Data); i += n { 55 for k := 0; k < n; k++ { 56 buf.Data[i+k] *= s[k] 57 s[k] *= 0.99999 58 } 59 } 60 } 61 } 62 63 func BenchmarkInterleaved2(b *testing.B) { 64 buf := NewInterleaved2(BufferSize) 65 for i := 0; i < b.N; i++ { 66 a, b := params() 67 for i := range buf.Data { 68 buf.Data[i][0] *= a 69 buf.Data[i][1] *= b 70 a *= 0.99999 71 b *= 0.99999 72 } 73 } 74 } 75 76 func BenchmarkDeinterleaved(b *testing.B) { 77 buf := NewDeinterleaved(BufferSize) 78 for i := 0; i < b.N; i++ { 79 a, b := params() 80 left := buf.Data[:len(buf.Data)/2] 81 for i := range left { 82 left[i] *= a 83 a *= 0.99999 84 } 85 right := buf.Data[len(buf.Data)/2:] 86 for i := range right { 87 right[i] *= b 88 b *= 0.99999 89 } 90 } 91 }