Created
September 1, 2016 14:47
-
-
Save rjammala/e47e6458413c5e920f2fb712f6943213 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bench.go | |
package dsp | |
import ( | |
"math" | |
) | |
func VScaleF32(input, output []float32, scale float32) { | |
n := len(input) | |
if len(output) < n { | |
n = len(output) | |
} | |
for i, v := range input[:n] { | |
output[i] = v * scale | |
} | |
} | |
func VMaxF32(input []float32) float32 { | |
max := float32(math.Inf(-1)) | |
for _, v := range input { | |
if v > max { | |
max = v | |
} | |
} | |
return max | |
} | |
// bench_test.go | |
package dsp | |
import ( | |
"math/rand" | |
"testing" | |
) | |
const benchSize = 1 << 12 | |
func BenchmarkVScaleF32(b *testing.B) { | |
input := make([]float32, benchSize) | |
output := make([]float32, len(input)) | |
b.SetBytes(benchSize) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
VScaleF32(input, output, 1.0/benchSize) | |
} | |
} | |
func BenchmarkVMaxF32(b *testing.B) { | |
input := make([]float32, benchSize) | |
rand.Seed(0) | |
for i := 0; i < len(input); i++ { | |
input[i] = rand.Float32() | |
} | |
b.SetBytes(benchSize) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
VMaxF32(input) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment