Skip to content

Commit

Permalink
Add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Nov 27, 2021
1 parent a084dca commit 8bf878e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
35 changes: 35 additions & 0 deletions benchmark/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"testing"
)

func addInt(a, b int) int {
return a + b
}

func addString(a, b string) string {
return a + b
}

func BenchmarkWithoutGenerics(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = addInt(1, 2)
_ = addString("foo", "bar")
}
}

type Addable interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64 | complex64 | complex128 | string
}

func add[T Addable](a, b T) T {
return a + b
}

func BenchmarkWithGenerics(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = add(1, 2)
_ = add("foo", "bar")
}
}
3 changes: 3 additions & 0 deletions benchmark/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/mattn/go-generics-example/benchmark

go 1.18

0 comments on commit 8bf878e

Please sign in to comment.