Skip to content

Commit

Permalink
Merge pull request #11 from azihsoyn/feature/add_benchmark
Browse files Browse the repository at this point in the history
add benchmark
  • Loading branch information
azihsoyn authored Oct 28, 2016
2 parents 885c1b1 + a88378c commit b8f8012
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 9 deletions.
43 changes: 43 additions & 0 deletions benchmark/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Benchmark

```
BenchmarkNew-8 20000000 89.4 ns/op 0 B/op 0 allocs/op
BenchmarkDistinct-8 100000 14808 ns/op 1043 B/op 34 allocs/op
BenchmarkDistinct_WithoutGollection-8 500000 3534 ns/op 339 B/op 2 allocs/op
BenchmarkDistinctBy-8 30000 49867 ns/op 1843 B/op 54 allocs/op
BenchmarkDistinctBy_WithoutGollection-8 300000 3928 ns/op 339 B/op 2 allocs/op
BenchmarkFilter-8 30000 41066 ns/op 1456 B/op 51 allocs/op
BenchmarkFilter_WithoutGollection-8 3000000 551 ns/op 160 B/op 1 allocs/op
BenchmarkFlatMap-8 30000 51068 ns/op 2144 B/op 69 allocs/op
BenchmarkFlatMap_WithoutGollection-8 1000000 1393 ns/op 368 B/op 4 allocs/op
BenchmarkFlatten-8 100000 15726 ns/op 1184 B/op 29 allocs/op
BenchmarkFlatten_WithoutGollection-8 1000000 1388 ns/op 368 B/op 4 allocs/op
BenchmarkFold-8 30000 45872 ns/op 1336 B/op 42 allocs/op
BenchmarkFold_WithoutGollection-8 5000000 254 ns/op 0 B/op 0 allocs/op
BenchmarkMap-8 30000 46036 ns/op 1840 B/op 63 allocs/op
BenchmarkMap_WithoutGollection-8 2000000 702 ns/op 160 B/op 1 allocs/op
BenchmarkReduce-8 30000 39491 ns/op 1272 B/op 40 allocs/op
BenchmarkReduce_WithoutGollection-8 5000000 240 ns/op 0 B/op 0 allocs/op
BenchmarkSort-8 10000 148140 ns/op 4224 B/op 127 allocs/op
BenchmarkSort_WithoutGollection-8 300000 5172 ns/op 32 B/op 1 allocs/op
BenchmarkTake-8 1000000 2483 ns/op 336 B/op 6 allocs/op
BenchmarkTake_WithoutGollection-8 2000000 701 ns/op 160 B/op 1 allocs/op
```

machine spec
```
$ system_profiler SPHardwareDataType
Hardware:
Hardware Overview:
Model Name: MacBook Pro
Model Identifier: MacBookPro11,4
Processor Name: Intel Core i7
Processor Speed: 2.2 GHz
Number of Processors: 1
Total Number of Cores: 4
L2 Cache (per Core): 256 KB
L3 Cache: 6 MB
Memory: 16 GB
```
214 changes: 214 additions & 0 deletions benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package gollection_test

import (
"sort"
"testing"

"github.com/azihsoyn/gollection"
)

func BenchmarkNew(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = gollection.New([]int{})
}
}

func BenchmarkDistinct(b *testing.B) {
g := gollection.New([]int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9})
for i := 0; i < b.N; i++ {
g.Distinct()
}
}

func BenchmarkDistinct_WithoutGollection(b *testing.B) {
arr := []int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9}
for i := 0; i < b.N; i++ {
m := make(map[int]bool)
ret := make([]int, 0, len(arr))
for _, i := range arr {
if _, ok := m[i]; !ok {
ret = append(ret, i)
m[i] = true
}
}
}
}
func BenchmarkDistinctBy(b *testing.B) {
g := gollection.New([]int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9})
for i := 0; i < b.N; i++ {
g.DistinctBy(func(v int) int {
return v
})
}
}
func BenchmarkDistinctBy_WithoutGollection(b *testing.B) {
arr := []int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9}
f := func(i int) int {
return i
}
for i := 0; i < b.N; i++ {
m := make(map[int]bool)
ret := make([]int, 0, len(arr))
for _, i := range arr {
id := f(i)
if _, ok := m[id]; !ok {
ret = append(ret, i)
m[id] = true
}
}
}
}

func BenchmarkFilter(b *testing.B) {
g := gollection.New([]int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9})
for i := 0; i < b.N; i++ {
g.Filter(func(v int) bool {
return v > 5
})
}
}

func BenchmarkFilter_WithoutGollection(b *testing.B) {
arr := []int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9}
for i := 0; i < b.N; i++ {
ret := make([]int, 0, len(arr))
for _, i := range arr {
if i > 5 {
ret = append(ret, i)
}
}
}
}

func BenchmarkFlatMap(b *testing.B) {
g := gollection.New([][]int{{0, 0, 1, 1, 2, 2}, {3, 3, 4, 4, 5, 5}, {6, 6, 7, 7, 8, 8, 9, 9}})
for i := 0; i < b.N; i++ {
g.FlatMap(func(v int) int {
return v * 2
})
}
}

func BenchmarkFlatMap_WithoutGollection(b *testing.B) {
arr := [][]int{{0, 0, 1, 1, 2, 2}, {3, 3, 4, 4, 5, 5}, {6, 6, 7, 7, 8, 8, 9, 9}}
for i := 0; i < b.N; i++ {
ret := make([]int, 0, len(arr))
for _, arr2 := range arr {
for _, v := range arr2 {
ret = append(ret, v*2)
}
}
}
}

func BenchmarkFlatten(b *testing.B) {
g := gollection.New([][]int{{0, 0, 1, 1, 2, 2}, {3, 3, 4, 4, 5, 5}, {6, 6, 7, 7, 8, 8, 9, 9}})
for i := 0; i < b.N; i++ {
g.Flatten()
}
}

func BenchmarkFlatten_WithoutGollection(b *testing.B) {
arr := [][]int{{0, 0, 1, 1, 2, 2}, {3, 3, 4, 4, 5, 5}, {6, 6, 7, 7, 8, 8, 9, 9}}
for i := 0; i < b.N; i++ {
ret := make([]int, 0, len(arr))
for _, arr2 := range arr {
for _, v := range arr2 {
ret = append(ret, v)
}
}
}
}

func BenchmarkFold(b *testing.B) {
g := gollection.New([]int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9})
for i := 0; i < b.N; i++ {
g.Fold(0, func(v1, v2 int) int {
return v1 + v2
})
}
}

func BenchmarkFold_WithoutGollection(b *testing.B) {
arr := []int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9}
for i := 0; i < b.N; i++ {
var ret int
for _, v := range arr {
ret += v
}
}
}

func BenchmarkMap(b *testing.B) {
g := gollection.New([]int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9})
for i := 0; i < b.N; i++ {
g.Map(func(v int) int {
return v * 2
})
}
}

func BenchmarkMap_WithoutGollection(b *testing.B) {
arr := []int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9}
for i := 0; i < b.N; i++ {
ret := make([]int, 0, len(arr))
for _, v := range arr {
ret = append(ret, v*2)
}
}
}
func BenchmarkReduce(b *testing.B) {
g := gollection.New([]int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9})
for i := 0; i < b.N; i++ {
g.Reduce(func(v1, v2 int) int {
return v1 + v2
})
}
}

func BenchmarkReduce_WithoutGollection(b *testing.B) {
arr := []int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9}
for i := 0; i < b.N; i++ {
var ret int
for _, v := range arr {
ret += v
}
}
}

func BenchmarkSort(b *testing.B) {
g := gollection.New([]int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9})
for i := 0; i < b.N; i++ {
g.SortBy(func(v1, v2 int) bool {
return v1 < v2
})
}
}

func BenchmarkSort_WithoutGollection(b *testing.B) {
arr := []int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9}
for i := 0; i < b.N; i++ {
sort.Sort(sort.IntSlice(arr))
}
}

func BenchmarkTake(b *testing.B) {
g := gollection.New([]int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9})
for i := 0; i < b.N; i++ {
g.Take(3)
}
}

func BenchmarkTake_WithoutGollection(b *testing.B) {
arr := []int{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9}
for i := 0; i < b.N; i++ {
limit := 3
if limit < len(arr) {
limit = len(arr)
}
ret := make([]int, 0, limit)
for i := 0; i < limit; i++ {
ret = append(ret, arr[i])
}
}
}
5 changes: 3 additions & 2 deletions distinct.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ func (g *gollection) Distinct() *gollection {

for i := 0; i < sv.Len(); i++ {
v := sv.Index(i)
if _, ok := m[v.Interface()]; !ok {
id := v.Interface()
if _, ok := m[id]; !ok {
ret = reflect.Append(ret, v)
m[v.Interface()] = true
m[id] = true
}
}

Expand Down
2 changes: 1 addition & 1 deletion filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (g *gollection) Filter(f interface{}) *gollection {
for i := 0; i < sv.Len(); i++ {
v := sv.Index(i)
if funcValue.Call([]reflect.Value{v})[0].Interface().(bool) {
ret = reflect.Append(ret, sv.Index(i))
ret = reflect.Append(ret, v)
}
}

Expand Down
9 changes: 3 additions & 6 deletions sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ func (g *gollection) SortBy(f interface{}) *gollection {
err: fmt.Errorf("gollection.SortBy called with non-slice value of type %T", g.slice),
}
}
orig := reflect.MakeSlice(sv.Type(), sv.Len(), sv.Cap())
ret := reflect.MakeSlice(sv.Type(), sv.Len(), sv.Cap())
reflect.Copy(orig, sv)
reflect.Copy(ret, sv)

funcValue := reflect.ValueOf(f)
funcType := funcValue.Type()
Expand All @@ -34,16 +33,14 @@ func (g *gollection) SortBy(f interface{}) *gollection {
}

less := func(i, j int) bool {
return funcValue.Call([]reflect.Value{sv.Index(i), sv.Index(j)})[0].Interface().(bool)
return funcValue.Call([]reflect.Value{ret.Index(i), ret.Index(j)})[0].Interface().(bool)
}

sort.Sort(&funcs{
length: sv.Len(),
less: less,
swap: reflectutil.Swapper(g.slice),
swap: reflectutil.Swapper(ret.Interface()),
})
reflect.Copy(ret, sv)
reflect.Copy(sv, orig)

return &gollection{
slice: ret.Interface(),
Expand Down

0 comments on commit b8f8012

Please sign in to comment.