Skip to content

Commit

Permalink
Add benchmark test
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen Quan committed Feb 22, 2021
1 parent ed07513 commit 2310a45
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 28 deletions.
28 changes: 0 additions & 28 deletions list/linked_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,34 +917,6 @@ func TestLinkedList_Delete(t *testing.T) {

}

// Benchmark
func BenchmarkNewLinkedList(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewLinkedList()
}
}
func BenchmarkLinkedList_Add(b *testing.B) {
list := NewLinkedList()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = list.Add(i)
}
}

func BenchmarkLinkedList_AddAll(b *testing.B) {
list := NewLinkedList()
listDefault := NewSliceListDefault()
for i := 0; i < 1; i++ {
_, _ = listDefault.Add(i)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = list.AddAll(listDefault)
}
}

func Test_itrLinkedList(t *testing.T) {
list := genLinkedList([]collection.Element{1, 2, 3, 4}...)
linkedList := &itrLinkedList{
Expand Down
115 changes: 115 additions & 0 deletions list/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2021 Chen Quan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package list

import (
"math/rand"
"testing"
)

// Benchmark
func BenchmarkNewLinkedList(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = NewLinkedList()
}
}
func BenchmarkNewSliceList(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = NewSliceListDefault()
}
}
func BenchmarkLinkedList_Add(b *testing.B) {
list := NewLinkedList()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = list.Add(i)
}
}

func BenchmarkSliceList_Add(b *testing.B) {
list := NewSliceListDefault()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = list.Add(i)
}
}
func BenchmarkLinkedList_AddAll(b *testing.B) {
list := NewLinkedList()
listDefault := NewSliceListDefault()
for i := 0; i < 100; i++ {
_, _ = listDefault.Add(i)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = list.AddAll(listDefault)
}
}
func BenchmarkSliceList_AddAll(b *testing.B) {
list := NewSliceListDefault()
listDefault := NewSliceListDefault()
for i := 0; i < 100; i++ {
_, _ = listDefault.Add(i)
}

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = list.AddAll(listDefault)
}
}

func BenchmarkLinkedList_AddIndex(b *testing.B) {
list := NewLinkedList()
rand.Seed(2020)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
n := rand.Int()

_ = list.AddIndex(i%n, 00)
}
}

func BenchmarkSliceList_AddIndex111(b *testing.B) {
list := NewSliceListDefault()
rand.Seed(2020)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
n := rand.Int()
_ = list.AddIndex(i%n, i)
}
}
func BenchmarkLinkedList_Get(b *testing.B) {
list := NewLinkedList()
rand.Seed(2020)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
n := rand.Int()
_, _ = list.Add(i)
_, _ = list.Get(i & n)
}
}

0 comments on commit 2310a45

Please sign in to comment.