Skip to content

Commit

Permalink
Add benchmarks for int16 for comparison.
Browse files Browse the repository at this point in the history
  • Loading branch information
lunemec committed Nov 3, 2022
1 parent e46ce4a commit 0c94cb0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
20 changes: 10 additions & 10 deletions as.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,43 +44,43 @@ func T[To Number](v any) (To, error) {
switch any(out).(type) {
case int:
var n int
n, err = Int(any(v))
n, err = Int(v)
out = To(n)
case int8:
var n int8
n, err = Int8(any(v))
n, err = Int8(v)
out = To(n)
case int16:
var n int16
n, err = Int16(any(v))
n, err = Int16(v)
out = To(n)
case int32:
var n int32
n, err = Int32(any(v))
n, err = Int32(v)
out = To(n)
case int64:
var n int64
n, err = Int64(any(v))
n, err = Int64(v)
out = To(n)
case uint:
var n uint
n, err = Uint(any(v))
n, err = Uint(v)
out = To(n)
case uint8:
var n uint8
n, err = Uint8(any(v))
n, err = Uint8(v)
out = To(n)
case uint16:
var n uint16
n, err = Uint16(any(v))
n, err = Uint16(v)
out = To(n)
case uint32:
var n uint32
n, err = Uint32(any(v))
n, err = Uint32(v)
out = To(n)
case uint64:
var n uint64
n, err = Uint64(any(v))
n, err = Uint64(v)
out = To(n)
default:
return out, InvalidTypeError{
Expand Down
23 changes: 23 additions & 0 deletions int16_64b_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,26 @@ func TestInt16(t *testing.T) {
assertNoError(t, as.Int16, uint(0))
assertError(t, as.Int16, uint(math.MaxUint64))
}

var out16 int16

// BenchmarkAs16-8 36798218 32.61 ns/op 39 B/op 1 allocs/op

func BenchmarkAs16(b *testing.B) {
var t int16
for n := 0; n < b.N; n++ {
t, _ = as.Int16(n)
}

out16 = t
}

// BenchmarkInt16-8 1000000000 0.3211 ns/op 0 B/op 0 allocs/op
func BenchmarkInt16(b *testing.B) {
var t int16
for n := 0; n < b.N; n++ {
t = int16(n)
}

out16 = t
}

0 comments on commit 0c94cb0

Please sign in to comment.