Skip to content

Commit

Permalink
新增stringx Benchmark (#216)
Browse files Browse the repository at this point in the history
* 新增stringx Benchmark

* 添加changelog
  • Loading branch information
Depravity-pig authored Sep 17, 2023
1 parent 7356bbf commit ee80839
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions .CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [sqlx: Scanner 添加 NextResultSet 方法](https://github.com/ecodeclub/ekit/pull/212)
- [ekit: AnyValue 支持As[Type]类型 String 转换](https://github.com/ecodeclub/ekit/pull/213)
- [stringx: unsafe 转换 string 和 []byte](https://github.com/ecodeclub/ekit/pull/215)
- [stringx: 添加 Benchmark](https://github.com/ecodeclub/ekit/pull/216)

# v0.0.7
- [slice: FilterDelete](https://github.com/ecodeclub/ekit/pull/152)
Expand Down
32 changes: 32 additions & 0 deletions stringx/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,35 @@ func TestUnsafeToString(t *testing.T) {
})
}
}

func Benchmark_UnsafeToBytes(b *testing.B) {
b.Run("safe to bytes", func(b *testing.B) {
s := "hello ekit! hello golang! this is test benchmark"
for i := 0; i < b.N; i++ {
_ = []byte(s)
}
})

b.Run("unsafe to bytes", func(b *testing.B) {
s := "hello ekit! hello golang! this is test benchmark"
for i := 0; i < b.N; i++ {
_ = UnsafeToBytes(s)
}
})
}

func Benchmark_UnsafeToString(b *testing.B) {
b.Run("safe to string", func(b *testing.B) {
s := []byte("hello ekit! hello golang! this is test benchmark")
for i := 0; i < b.N; i++ {
_ = string(s)
}
})

b.Run("unsafe to string", func(b *testing.B) {
s := []byte("hello ekit! hello golang! this is test benchmark")
for i := 0; i < b.N; i++ {
_ = UnsafeToString(s)
}
})
}
10 changes: 10 additions & 0 deletions stringx/stringx_benchmark
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
goos: darwin
goarch: amd64
pkg: github.com/ecodeclub/ekit/stringx
cpu: Intel(R) Core(TM) i7-7920HQ CPU @ 3.10GHz
Benchmark_UnsafeToBytes/safe_to_bytes-8 39721614 29.60 ns/op 48 B/op 1 allocs/op
Benchmark_UnsafeToBytes/unsafe_to_bytes-8 1000000000 0.2805 ns/op 0 B/op 0 allocs/op
Benchmark_UnsafeToString/safe_to_string-8 45207981 26.77 ns/op 48 B/op 1 allocs/op
Benchmark_UnsafeToString/unsafe_to_string-8 1000000000 0.2842 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/ecodeclub/ekit/stringx 4.780s

0 comments on commit ee80839

Please sign in to comment.