-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A lot of changes you may find useful #3
base: main
Are you sure you want to change the base?
Changes from all commits
9bcd03f
acffdb4
f169279
62860e6
446ab90
0975c39
3597796
a5f4ebf
717ff45
dca9347
cb46783
d561c6f
a3b1595
a73b215
c5c95aa
49e7ab7
427d6aa
2c7155b
c62959a
aed1c31
b4542c6
bfbed65
bd23383
e202a0e
0a220b4
ce8afca
21ab2dd
a68ded5
c29fab6
a4ddefc
bfd1fe3
b4e3011
dfbd4bf
25e2cc3
90ded3b
d680d36
a81d4f7
7ec6c14
f32906a
c33b8bc
96511ec
ff0f752
a706f88
62d5e9b
be0597b
2facace
af6db31
8ab0a34
aba5a45
e80bbb9
6405290
1c4f639
74baa67
6770edb
0a4fd75
752bf49
f06165c
f49be6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -12,64 +12,106 @@ var addResultMFloat64 MVector[float64] | |||||||||||
|
||||||||||||
func BenchmarkAddVector(b *testing.B) { | ||||||||||||
var r vector3.Vector[float64] | ||||||||||||
a := vector3.New(1., 2., 3.) | ||||||||||||
va := vector3.New(1., 2., 3.) | ||||||||||||
vb := vector3.New(2., 3., 4.) | ||||||||||||
Comment on lines
-15
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move variables inside for loop, and have them take into account This goes for all other instances of this in this file |
||||||||||||
|
||||||||||||
for n := 0; n < b.N; n++ { | ||||||||||||
r = r.Add(a) | ||||||||||||
r = r.Add(va).MultByVector(va). | ||||||||||||
Add(va.Reciprocal()).MultByVector(va.Reciprocal()). | ||||||||||||
MultByVector(vb.Reciprocal()).Add(vb.Reciprocal()) | ||||||||||||
} | ||||||||||||
addResultFloat64 = r | ||||||||||||
runtime.KeepAlive(r) | ||||||||||||
} | ||||||||||||
|
||||||||||||
func BenchmarkAddMutableVector(b *testing.B) { | ||||||||||||
var r MVector[float64] | ||||||||||||
a := MVector[float64]{1., 2., 3.} | ||||||||||||
va := MVector[float64]{1., 2., 3.} | ||||||||||||
vb := MVector[float64]{2., 3., 4.} | ||||||||||||
|
||||||||||||
for n := 0; n < b.N; n++ { | ||||||||||||
r = r.Add(a) | ||||||||||||
r = r.Add(va).MultByVector(va). | ||||||||||||
Add(va.Reciprocal()).MultByVector(va.Reciprocal()). | ||||||||||||
MultByVector(vb.Reciprocal()).Add(vb.Reciprocal()) | ||||||||||||
} | ||||||||||||
addResultMFloat64 = r | ||||||||||||
runtime.KeepAlive(r) | ||||||||||||
} | ||||||||||||
|
||||||||||||
func BenchmarkAddMutableVectorInPlace(b *testing.B) { | ||||||||||||
var r MVector[float64] | ||||||||||||
a := MVector[float64]{1., 2., 3.} | ||||||||||||
va := MVector[float64]{1., 2., 3.} | ||||||||||||
vb := MVector[float64]{2., 3., 4.} | ||||||||||||
for n := 0; n < b.N; n++ { | ||||||||||||
r.AddInPlace(a) | ||||||||||||
r.AddInPlace(va) | ||||||||||||
r.MultInPlace(va) | ||||||||||||
r.AddInPlace(va.Reciprocal()) | ||||||||||||
r.MultInPlace(va.Reciprocal()) | ||||||||||||
r.MultInPlace(vb.Reciprocal()) | ||||||||||||
r.AddInPlace(vb.Reciprocal()) | ||||||||||||
} | ||||||||||||
addResultMFloat64 = r | ||||||||||||
runtime.KeepAlive(r) | ||||||||||||
} | ||||||||||||
|
||||||||||||
func BenchmarkAddMutableVectorInPlaceUsingPointer(b *testing.B) { | ||||||||||||
var r *MVector[float64] = &MVector[float64]{1, 2, 3} | ||||||||||||
a := MVector[float64]{1., 2., 3.} | ||||||||||||
va := MVector[float64]{1., 2., 3.} | ||||||||||||
vb := MVector[float64]{2., 3., 4.} | ||||||||||||
|
||||||||||||
for n := 0; n < b.N; n++ { | ||||||||||||
r.AddInPlace(a) | ||||||||||||
r.AddInPlace(va) | ||||||||||||
r.MultInPlace(va) | ||||||||||||
r.AddInPlace(va.Reciprocal()) | ||||||||||||
r.MultInPlace(va.Reciprocal()) | ||||||||||||
r.MultInPlace(vb.Reciprocal()) | ||||||||||||
r.AddInPlace(vb.Reciprocal()) | ||||||||||||
} | ||||||||||||
|
||||||||||||
addResultMFloat64 = *r | ||||||||||||
runtime.KeepAlive(r) | ||||||||||||
} | ||||||||||||
|
||||||||||||
func BenchmarkAddMutableVectorInPlaceDirect(b *testing.B) { | ||||||||||||
var r MVector[float64] | ||||||||||||
va := MVector[float64]{1., 2., 3.} | ||||||||||||
vb := MVector[float64]{2., 3., 4.} | ||||||||||||
for n := 0; n < b.N; n++ { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. factor out the reciprocal to make it 1:1 in the immutable use-case to make it a fair comparison |
||||||||||||
r.X = ((r.X+va.X)*va.X+1/va.X)/va.X/vb.X + 1/vb.X | ||||||||||||
r.Y = ((r.Y+va.Y)*va.Y+1/va.Y)/va.Y/vb.Y + 1/vb.Y | ||||||||||||
} | ||||||||||||
Comment on lines
+81
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the z component to make it a fair benchmark
Suggested change
|
||||||||||||
addResultMFloat64 = r | ||||||||||||
runtime.KeepAlive(r) | ||||||||||||
} | ||||||||||||
|
||||||||||||
func BenchmarkAddMutableVectorInPlaceWithReturn(b *testing.B) { | ||||||||||||
var r MVector[float64] | ||||||||||||
a := MVector[float64]{1., 2., 3.} | ||||||||||||
va := MVector[float64]{1., 2., 3.} | ||||||||||||
vb := MVector[float64]{2., 3., 4.} | ||||||||||||
for n := 0; n < b.N; n++ { | ||||||||||||
r.AddInPlaceAndReturn(a) | ||||||||||||
r.AddInPlaceAndReturn(va) | ||||||||||||
r.MultInPlaceAndReturn(va) | ||||||||||||
r.AddInPlaceAndReturn(va.Reciprocal()) | ||||||||||||
r.MultInPlaceAndReturn(va.Reciprocal()) | ||||||||||||
r.MultInPlaceAndReturn(vb.Reciprocal()) | ||||||||||||
r.AddInPlaceAndReturn(vb.Reciprocal()) | ||||||||||||
} | ||||||||||||
addResultMFloat64 = r | ||||||||||||
runtime.KeepAlive(r) | ||||||||||||
} | ||||||||||||
|
||||||||||||
func BenchmarkAddMutableVectorInPlaceWithReturnUsingPointer(b *testing.B) { | ||||||||||||
var r *MVector[float64] = &MVector[float64]{1, 2, 3} | ||||||||||||
a := MVector[float64]{1., 2., 3.} | ||||||||||||
va := MVector[float64]{1., 2., 3.} | ||||||||||||
vb := MVector[float64]{2., 3., 4.} | ||||||||||||
|
||||||||||||
for n := 0; n < b.N; n++ { | ||||||||||||
r.AddInPlaceAndReturn(a) | ||||||||||||
r.AddInPlaceAndReturn(va) | ||||||||||||
r.MultInPlaceAndReturn(va) | ||||||||||||
r.AddInPlaceAndReturn(va.Reciprocal()) | ||||||||||||
r.MultInPlaceAndReturn(va.Reciprocal()) | ||||||||||||
r.MultInPlaceAndReturn(vb.Reciprocal()) | ||||||||||||
r.AddInPlaceAndReturn(vb.Reciprocal()) | ||||||||||||
} | ||||||||||||
|
||||||||||||
addResultMFloat64 = *r | ||||||||||||
|
@@ -78,12 +120,21 @@ func BenchmarkAddMutableVectorInPlaceWithReturnUsingPointer(b *testing.B) { | |||||||||||
|
||||||||||||
func BenchmarkAddMutableVectorInPlaceTakingPointerUsingPointer(b *testing.B) { | ||||||||||||
var r *MVector[float64] = &MVector[float64]{1, 2, 3} | ||||||||||||
a := &MVector[float64]{1., 2., 3.} | ||||||||||||
va := &MVector[float64]{1., 2., 3.} | ||||||||||||
vb := &MVector[float64]{2., 3., 4.} | ||||||||||||
|
||||||||||||
for n := 0; n < b.N; n++ { | ||||||||||||
r.AddInPlaceTakingPointer(a) | ||||||||||||
ra := va.Reciprocal() | ||||||||||||
rb := vb.Reciprocal() | ||||||||||||
r.AddInPlaceTakingPointer(va) | ||||||||||||
r.MultInPlaceTakingPointer(va) | ||||||||||||
r.AddInPlaceTakingPointer(&ra) | ||||||||||||
r.MultInPlaceTakingPointer(&ra) | ||||||||||||
r.MultInPlaceTakingPointer(&rb) | ||||||||||||
r.AddInPlaceTakingPointer(&rb) | ||||||||||||
} | ||||||||||||
|
||||||||||||
addResultMFloat64 = *r | ||||||||||||
runtime.KeepAlive(r) | ||||||||||||
runtime.KeepAlive(addResultMFloat64) | ||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package experiments_test | ||
|
||
import ( | ||
"math/rand" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/EliCDavis/vector" | ||
"github.com/EliCDavis/vector/mathex" | ||
"github.com/EliCDavis/vector/vector2" | ||
"golang.org/x/exp/constraints" | ||
) | ||
|
||
type Number interface { | ||
constraints.Integer | constraints.Float | vector.Number | ||
} | ||
|
||
var r = rand.New(rand.NewSource(99)) | ||
|
||
func ClampGenericMinMax[T Number](f, vmin, vmax T) T { | ||
return max(min(f, vmax), vmin) | ||
} | ||
|
||
func ClampGenericCompare[T Number](f, vmin, vmax T) T { | ||
if f <= vmin { | ||
return vmin | ||
} | ||
if f >= vmax { | ||
return vmax | ||
} | ||
return f | ||
} | ||
|
||
func ClampMinMax(f, vmin, vmax float64) float64 { | ||
return max(min(f, vmax), vmin) | ||
} | ||
|
||
func ClampCompare(f, vmin, vmax float64) float64 { | ||
if f <= vmin { | ||
return vmin | ||
} | ||
if f >= vmax { | ||
return vmax | ||
} | ||
return f | ||
} | ||
|
||
func BenchmarkClampMathEx(b *testing.B) { | ||
var res int | ||
i := r.Int() | ||
clamp := vector2.New(r.Int(), r.Int()) | ||
imin := clamp.MinComponent() | ||
imax := clamp.MaxComponent() | ||
b.ResetTimer() | ||
|
||
for n := 0; n < b.N; n++ { | ||
res += mathex.Clamp(i, imin, imax) | ||
} | ||
runtime.KeepAlive(res) | ||
} | ||
|
||
func BenchmarkClampGenericMinMax(b *testing.B) { | ||
var res int | ||
i := r.Int() | ||
clamp := vector2.New(r.Int(), r.Int()) | ||
imin := clamp.MinComponent() | ||
imax := clamp.MaxComponent() | ||
b.ResetTimer() | ||
|
||
for n := 0; n < b.N; n++ { | ||
res += ClampGenericMinMax(i, imin, imax) | ||
} | ||
runtime.KeepAlive(res) | ||
} | ||
|
||
func BenchmarkClampGenericCompare(b *testing.B) { | ||
var res int | ||
i := r.Int() | ||
clamp := vector2.New(r.Int(), r.Int()) | ||
imin := clamp.MinComponent() | ||
imax := clamp.MaxComponent() | ||
b.ResetTimer() | ||
|
||
for n := 0; n < b.N; n++ { | ||
res += ClampGenericCompare(i, imin, imax) | ||
} | ||
runtime.KeepAlive(res) | ||
} | ||
|
||
func BenchmarkClampMinMax(b *testing.B) { | ||
var res int | ||
i := r.Int() | ||
clamp := vector2.New(r.Int(), r.Int()) | ||
imin := clamp.MinComponent() | ||
imax := clamp.MaxComponent() | ||
b.ResetTimer() | ||
|
||
for n := 0; n < b.N; n++ { | ||
res += (int)(ClampMinMax(float64(i), float64(imin), float64(imax))) | ||
} | ||
|
||
runtime.KeepAlive(res) | ||
} | ||
|
||
func BenchmarkClampCompare(b *testing.B) { | ||
var res int | ||
i := r.Int() | ||
clamp := vector2.New(r.Int(), r.Int()) | ||
imin := clamp.MinComponent() | ||
imax := clamp.MaxComponent() | ||
b.ResetTimer() | ||
|
||
for n := 0; n < b.N; n++ { | ||
res += (int)(ClampCompare(float64(i), float64(imin), float64(imax))) | ||
} | ||
|
||
runtime.KeepAlive(res) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
module github.com/EliCDavis/vector | ||
|
||
go 1.19 | ||
go 1.21 | ||
igadmg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
require github.com/stretchr/testify v1.8.1 | ||
require github.com/stretchr/testify v1.9.0 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,12 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= | ||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 h1:ESSUROHIBHg7USnszlcdmjBEwdMj9VUvU+OPk4yl2mc= | ||
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move changes from this benchmark into a new file and benchmark. To improve it, it'd be cool to include more implemented functions in the vector libs