Skip to content

Commit

Permalink
Merge pull request #2 from NDari/master
Browse files Browse the repository at this point in the history
Add benchmarking script
  • Loading branch information
chewxy committed Nov 11, 2017
2 parents 1f59516 + 5bd0436 commit 0f5e85e
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ _testmain.go
*.exe
*.test
*.prof

vendor
vendor/*
33 changes: 33 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[[constraint]]
name = "github.com/chewxy/math32"
version = "1.0.0"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.1.4"
151 changes: 151 additions & 0 deletions arith_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// +build !sse,!avx

package vecf32

import (
"testing"

"github.com/chewxy/math32"
)

/* BENCHMARKS */

func _vanillaVecAdd(a, b []float32) {
for i := range a {
a[i] += b[i]
}
}

func BenchmarkVecAdd(b *testing.B) {
x := Range(0, niceprime)
y := Range(niceprime, 2*niceprime)

for n := 0; n < b.N; n++ {
Add(x, y)
}
}

func BenchmarkVanillaVecAdd(b *testing.B) {
x := Range(0, niceprime)
y := Range(niceprime, 2*niceprime)

for n := 0; n < b.N; n++ {
_vanillaVecAdd(x, y)
}
}

func _vanillaVecSub(a, b []float32) {
for i := range a {
a[i] -= b[i]
}
}

func BenchmarkVecSub(b *testing.B) {
x := Range(0, niceprime)
y := Range(niceprime, 2*niceprime)

for n := 0; n < b.N; n++ {
Sub(x, y)
}
}

func BenchmarkVanillaVecSub(b *testing.B) {
x := Range(0, niceprime)
y := Range(niceprime, 2*niceprime)

for n := 0; n < b.N; n++ {
_vanillaVecSub(x, y)
}
}

func _vanillaVecMul(a, b []float32) {
for i := range a {
a[i] *= b[i]
}
}

func BenchmarkVecMul(b *testing.B) {
x := Range(0, niceprime)
y := Range(niceprime, 2*niceprime)

for n := 0; n < b.N; n++ {
Mul(x, y)
}
}

func BenchmarkVanillaVecMul(b *testing.B) {
x := Range(0, niceprime)
y := Range(niceprime, 2*niceprime)

for n := 0; n < b.N; n++ {
_vanillaVecMul(x, y)
}
}

func _vanillaVecDiv(a, b []float32) {
for i := range a {
a[i] /= b[i]
}
}

func BenchmarkVecDiv(b *testing.B) {
x := Range(0, niceprime)
y := Range(niceprime, 2*niceprime)

for n := 0; n < b.N; n++ {
Div(x, y)
}
}

func BenchmarkVanillaVecDiv(b *testing.B) {
x := Range(0, niceprime)
y := Range(niceprime, 2*niceprime)

for n := 0; n < b.N; n++ {
_vanillaVecDiv(x, y)
}
}

func _vanillaVecSqrt(a []float32) {
for i, v := range a {
a[i] = math32.Sqrt(v)
}
}

func BenchmarkVecSqrt(b *testing.B) {
x := Range(0, niceprime)

for n := 0; n < b.N; n++ {
Sqrt(x)
}
}

func BenchmarkVanillaVecSqrt(b *testing.B) {
x := Range(0, niceprime)

for n := 0; n < b.N; n++ {
_vanillaVecSqrt(x)
}
}

func _vanillaVecInverseSqrt(a []float32) {
for i, v := range a {
a[i] = 1.0 / math32.Sqrt(v)
}
}

func BenchmarkVecInvSqrt(b *testing.B) {
x := Range(0, niceprime)

for n := 0; n < b.N; n++ {
InvSqrt(x)
}
}

func BenchmarkVanillaVecInvSqrt(b *testing.B) {
x := Range(0, niceprime)

for n := 0; n < b.N; n++ {
_vanillaVecInverseSqrt(x)
}
}
4 changes: 2 additions & 2 deletions arith_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func TestMinOf(t *testing.T) {
correct := float32(0)
got := MinOf(a)
if got != correct {
t.Error("Expected %f. Got %v instead", correct, got)
t.Errorf("Expected %f. Got %v instead", correct, got)
}

a = []float32{}
Expand All @@ -313,7 +313,7 @@ func TestArgmax(t *testing.T) {
correct := 3
got := Argmax(a)
if got != correct {
t.Error("Expected argmax to be %v. Got %v instead", correct, got)
t.Errorf("Expected argmax to be %v. Got %v instead", correct, got)
}

a = []float32{math32.Inf(-1), 2, 3, 4}
Expand Down
17 changes: 17 additions & 0 deletions bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
set -ex

benchtime=${1:-1s}

go test -bench . -benchtime $benchtime
go test -tags='sse' -bench . -benchtime $benchtime
go test -tags='avx' -bench . -benchtime $benchtime

# travis compiles commands in script and then executes in bash. By adding
# set -e we are changing the travis build script's behavior, and the set
# -e lives on past the commands we are providing it. Some of the travis
# commands are supposed to exit with non zero status, but then continue
# executing. set -x makes the travis log files extremely verbose and
# difficult to understand.
#
# see travis-ci/travis-ci#5120
set +ex

0 comments on commit 0f5e85e

Please sign in to comment.