mirror of
https://github.com/samber/lo.git
synced 2025-09-26 20:11:13 +08:00
adding benchmarks
This commit is contained in:
@@ -3,6 +3,6 @@ FROM golang:1.18rc1-bullseye
|
||||
|
||||
WORKDIR /go/src/github.com/samber/lo
|
||||
|
||||
COPY Makefile go.* /go/src/github.com/samber/lo
|
||||
COPY Makefile go.* /go/src/github.com/samber/lo/
|
||||
|
||||
RUN make tools
|
||||
|
4
Makefile
4
Makefile
@@ -15,7 +15,9 @@ watch-test:
|
||||
reflex -R assets.go -t 50ms -s -- sh -c 'gotest -v ./...'
|
||||
|
||||
bench:
|
||||
gotest -benchmem -bench ./...
|
||||
go test -benchmem -count 3 -bench ./...
|
||||
watch-bench:
|
||||
reflex -R assets.go -t 50ms -s -- sh -c 'go test -benchmem -count 3 -bench ./...'
|
||||
|
||||
coverage:
|
||||
${BIN} test -v -coverprofile cover.out .
|
||||
|
37
README.md
37
README.md
@@ -574,9 +574,42 @@ ptr := lo.ToSlicePtr[string]([]string{"hello", "world"})
|
||||
// []*string{"hello", "world"}
|
||||
```
|
||||
|
||||
## 🛩 Performance
|
||||
## 🛩 Benchmark
|
||||
|
||||
We executed a simple benchmark with the a dead-simple `lo.Map` loop:
|
||||
|
||||
See the full implementation [here](./benchmark_test.go).
|
||||
|
||||
```go
|
||||
_ = lo.Map[int64](arr, func(x int64, i int) string {
|
||||
return strconv.FormatInt(x, 10)
|
||||
})
|
||||
```
|
||||
|
||||
**Result:**
|
||||
|
||||
Here is a comparison between `lo.Map`, `lop.Map`, `go-funk` library and a simple Go `for` loop.
|
||||
|
||||
```
|
||||
$ go test -benchmem -bench ./...
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/samber/lo
|
||||
cpu: Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
|
||||
BenchmarkMap/lo.Map-8 8 131041218 ns/op 39998055 B/op 1000001 allocs/op
|
||||
BenchmarkMap/lop.Map-8 2 509805437 ns/op 120018304 B/op 3000214 allocs/op
|
||||
BenchmarkMap/reflect-8 2 840512374 ns/op 170325888 B/op 4000042 allocs/op
|
||||
BenchmarkMap/for-8 9 127074794 ns/op 39998040 B/op 1000001 allocs/op
|
||||
PASS
|
||||
ok github.com/samber/lo 7.531s
|
||||
```
|
||||
|
||||
- `lo.Map` is way faster (x7) than `go-funk`, a relection-based Map implementation.
|
||||
- `lo.Map` have the same allocation profile than `for`.
|
||||
- `lo.Map` is 4% slower than `for`.
|
||||
- `lop.Map` is slower than `lo.Map` because it implies more memory allocation and locks. `lop.Map` will be usefull for long-running callbacks, such as i/o bound processing.
|
||||
- `for` beats other implementations for memory and CPU.
|
||||
|
||||
// TODO
|
||||
|
||||
## 🤝 Contributing
|
||||
|
||||
|
1
go.mod
1
go.mod
@@ -7,5 +7,6 @@ require github.com/stretchr/testify v1.7.0
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/thoas/go-funk v0.9.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@@ -3,9 +3,13 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
||||
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/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M=
|
||||
github.com/thoas/go-funk v0.9.1/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
|
||||
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.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
Reference in New Issue
Block a user