-
-
Save chewxy/058ac09a1a043068462feb20af3067d5 to your computer and use it in GitHub Desktop.
benchmark_array.go
This file contains 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
package bench | |
import ( | |
"math/rand" | |
"reflect" | |
"testing" | |
"testing/quick" | |
"time" | |
"unsafe" | |
"github.com/chewxy/gorgonia/tensor/internal/storage" | |
) | |
type float64ser interface { | |
float64s() []float64 | |
} | |
type array struct { | |
V interface{} | |
} | |
func (a *array) float64s_0() []float64 { | |
switch vt := a.V.(type) { | |
case []float64: | |
return vt | |
case []float32: | |
panic("HELP") | |
} | |
return a.V.([]float64) | |
} | |
func (a *array) float64s_1() []float64 { | |
switch vt := a.V.(type) { | |
case float64ser: | |
return vt.float64s() | |
case []float64: | |
return vt | |
case []float32: | |
panic("HELP") | |
} | |
return a.V.([]float64) | |
} | |
func newhdr(x interface{}) *storage.Header { | |
xT := reflect.TypeOf(x) | |
if xT.Kind() != reflect.Slice { | |
panic("Expected a slice") | |
} | |
xV := reflect.ValueOf(x) | |
ptr := xV.Pointer() | |
uptr := unsafe.Pointer(ptr) | |
return &storage.Header{ | |
Ptr: uptr, | |
L: xV.Len(), | |
C: xV.Cap(), | |
} | |
} | |
func BenchmarkTypecasting(b *testing.B) { | |
var a float64 | |
r := rand.New(rand.NewSource(time.Now().UnixNano())) | |
v, _ := quick.Value(reflect.TypeOf(a), r) | |
vf64 := v.Interface().(float64) | |
s := make([]float64, 4096) | |
for i := range s { | |
s[i] = vf64 | |
} | |
arr := newhdr(s) | |
// c := make([]float64, len(s)) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
f := arr.Float64s() | |
// copy(c, f) | |
GLOBAL2 = f | |
} | |
if len(GLOBAL2) != len(s) { | |
b.Errorf("Failed") | |
} | |
for i := range GLOBAL2 { | |
if GLOBAL2[i] != s[i] { | |
b.Errorf("FAILED2") | |
} | |
} | |
} | |
var GLOBAL1, GLOBAL2, GLOBAL3 []float64 | |
func BenchmarkTypeSwitchTypesOnly(b *testing.B) { | |
var a float64 | |
r := rand.New(rand.NewSource(time.Now().UnixNano())) | |
v, _ := quick.Value(reflect.TypeOf(a), r) | |
vf64 := v.Interface().(float64) | |
s := make([]float64, 4096) | |
for i := range s { | |
s[i] = vf64 | |
} | |
arr := &array{s} | |
// c := make([]float64, len(s)) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
f := arr.float64s_0() | |
GLOBAL1 = f | |
} | |
if len(GLOBAL1) != len(s) { | |
b.Errorf("Failed") | |
} | |
for i := range GLOBAL1 { | |
if GLOBAL1[i] != s[i] { | |
b.Errorf("FAILED2") | |
} | |
} | |
} | |
func BenchmarkTypeSwitchE2I2(b *testing.B) { | |
var a float64 | |
r := rand.New(rand.NewSource(time.Now().UnixNano())) | |
v, _ := quick.Value(reflect.TypeOf(a), r) | |
vf64 := v.Interface().(float64) | |
s := make([]float64, 4096) | |
for i := range s { | |
s[i] = vf64 | |
} | |
arr := &array{s} | |
// c := make([]float64, len(s)) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
f := arr.float64s_1() | |
GLOBAL3 = f | |
} | |
if len(GLOBAL3) != len(s) { | |
b.Errorf("Failed") | |
} | |
for i := range GLOBAL3 { | |
if GLOBAL3[i] != s[i] { | |
b.Errorf("FAILED2") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment