-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from azihsoyn/feature/add_benchmark
add benchmark
- Loading branch information
Showing
5 changed files
with
264 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters