Skip to content

Commit

Permalink
Benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsombre committed Jan 11, 2024
1 parent 289a360 commit 0f1a79f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 9 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ Currently, the framework is written in Golang 1.19.x as it is closer to 1.18.x a
```shell
go test -cover ./...
```

### Benchmarks

```shell
go test -bench=. -benchmem -run=^$ > bench.out
```
11 changes: 11 additions & 0 deletions all_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

// Benchmarks

var (
GlobalB bool
GlobalI int
GlobalF float64

GlobalPoint Point
)
9 changes: 9 additions & 0 deletions bench.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
goos: linux
goarch: amd64
pkg: github.com/mrsombre/codingame-framework
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
BenchmarkLine_IsCollision-8 271175239 4.403 ns/op 0 B/op 0 allocs/op
BenchmarkLine_LinesIntersection-8 351207843 3.375 ns/op 0 B/op 0 allocs/op
BenchmarkLine_IsPointOnLine-8 515632399 2.412 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/mrsombre/codingame-framework 4.666s
20 changes: 12 additions & 8 deletions geometry_line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,6 @@ func TestLine_IsPointOnLine(t *testing.T) {
}
}

func BenchmarkLine_IsPointOnLine(b *testing.B) {
line := Line{Point{0, 0}, Point{300, 300}}
point := Point{150, 150}
for i := 0; i < b.N; i++ {
line.IsPointOnLine(point)
}
}

func TestLine_IsPointOnSegment(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -601,3 +593,15 @@ func TestNewLine(t *testing.T) {
ln := NewLine(Point{0, 0}, Point{300, 400})
assert.Equal(t, Line{Point{0, 0}, Point{300, 400}}, ln)
}

// Benchmarks

func BenchmarkLine_IsCollision(b *testing.B) {
r := false
al := Line{Point{0, 0}, Point{600, 800}}
bl := Line{Point{300, 400}, Point{150, 200}}
for i := 0; i < b.N; i++ {
r = al.IsCollision(bl, 50)
}
GlobalB = r
}
6 changes: 6 additions & 0 deletions geometry_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ const (
topRight = 5
bottomRight = 6
bottomLeft = 7

// 0-corners
topLeft0 = 0
topRight0 = 1
bottomRight0 = 2
bottomLeft0 = 3
)

// Point represents a point in a 2D plane.
Expand Down
6 changes: 6 additions & 0 deletions geometry_point_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ func TestPoint_Index(t *testing.T) {
width: 10,
want: 0,
},
{
name: `last in row`,
p: Point{9, 0},
width: 10,
want: 9,
},
{
name: `next row`,
p: Point{0, 1},
Expand Down
18 changes: 17 additions & 1 deletion geometry_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,26 @@ import (
"testing"
)

// Benchmarks

func BenchmarkLine_LinesIntersection(b *testing.B) {
p := Point{}
r := false
al := Line{Point{0, 0}, Point{300, 400}}
bl := Line{Point{0, 400}, Point{300, 0}}
for i := 0; i < b.N; i++ {
linesIntersection(al, bl, true, true)
p, r = linesIntersection(al, bl, true, true)
}
GlobalPoint = p
GlobalB = r
}

func BenchmarkLine_IsPointOnLine(b *testing.B) {
r := false
line := Line{Point{0, 0}, Point{300, 300}}
point := Point{150, 150}
for i := 0; i < b.N; i++ {
r = isPointOnLine(line, point, true)
}
GlobalB = r
}

0 comments on commit 0f1a79f

Please sign in to comment.