DylanMeeus / GoAudio

Go tools for audio processing & creation 🎶
MIT License
350 stars 39 forks source link

bitsToInt optimization #27

Open hollowness-inside opened 6 months ago

hollowness-inside commented 6 months ago

Creating a new bytes reader with each function call is inefficient as it requires a lot of allocations and performs a lot of operations. The needed numeric types can be retrieved simply by utilizing bitwise operations. This approach is good in performance and the code looks simpler and cleaner.

I ran benchmarks on the Bits24ToIntReader function and the provided optimized. Here are my results:

goos: windows
goarch: amd64
pkg: bitsToInt
cpu: 12th Gen Intel(R) Core(TM) i7-12650H
BenchmarkReader-16       2183080               522.5 ns/op           576 B/op         36 allocs/op
BenchmarkBitwise-16     430430271                2.823 ns/op           0 B/op          0 allocs/op
BenchmarkCast-16        450972691                2.645 ns/op           0 B/op          0 allocs/op

And here are the functions that I benchmarked: