Peter H. Froehlich 12 лет назад
Родитель
Сommit
35a257f24d
2 измененных файлов с 87 добавлено и 0 удалено
  1. 18 0
      README.md
  2. 69 0
      queue_test.go

+ 18 - 0
README.md

@@ -19,3 +19,21 @@ insertion.
 
 I am trying to stick close to the conventions container/list seems
 to follow even though I disagree with several of them.
+
+## Latest Performance Comparison
+
+The benchmarks are not very sophisticated yet but it seems that we
+rather clearly beat container/list on the most common operations.
+
+```
+$ go test -bench . -benchmem -cover
+PASS
+BenchmarkPushFrontQueue	20000000	       191 ns/op	      53 B/op	       0 allocs/op
+BenchmarkPushFrontList	10000000	       290 ns/op	      49 B/op	       1 allocs/op
+BenchmarkPushBackQueue	10000000	       171 ns/op	      53 B/op	       0 allocs/op
+BenchmarkPushBackList	 5000000	       305 ns/op	      49 B/op	       1 allocs/op
+BenchmarkRandomQueue	 5000000	       418 ns/op	      26 B/op	       0 allocs/op
+BenchmarkRandomList	 2000000	       799 ns/op	      78 B/op	       1 allocs/op
+coverage: 84.1% of statements
+ok  	github.com/phf/go-queue	17.092s
+```

+ 69 - 0
queue_test.go

@@ -9,6 +9,8 @@ package queue
 // Go's container.list
 
 import "testing"
+import "container/list"
+import "math/rand"
 
 func ensureEmpty(t *testing.T, q *Queue) {
 	if l := q.Len(); l != 0 {
@@ -96,3 +98,70 @@ func TestZeroValue(t *testing.T) {
 	q.PushFront(5)
 	ensureLength(t, &q, 5)
 }
+
+func BenchmarkPushFrontQueue(b *testing.B) {
+	var q Queue
+	for i := 0; i < b.N; i++ {
+		q.PushFront(i)
+	}
+}
+func BenchmarkPushFrontList(b *testing.B) {
+	var q list.List
+	for i := 0; i < b.N; i++ {
+		q.PushFront(i)
+	}
+}
+
+func BenchmarkPushBackQueue(b *testing.B) {
+	var q Queue
+	for i := 0; i < b.N; i++ {
+		q.PushBack(i)
+	}
+}
+func BenchmarkPushBackList(b *testing.B) {
+	var q list.List
+	for i := 0; i < b.N; i++ {
+		q.PushBack(i)
+	}
+}
+
+func BenchmarkRandomQueue(b *testing.B) {
+	var q Queue
+	rand.Seed(64738)
+	for i := 0; i < b.N; i++ {
+		if rand.Float32() < 0.8 {
+			q.PushBack(i)
+		}
+		if rand.Float32() < 0.8 {
+			q.PushFront(i)
+		}
+		if rand.Float32() < 0.5 {
+			q.PopFront()
+		}
+		if rand.Float32() < 0.5 {
+			q.PopBack()
+		}
+	}
+}
+func BenchmarkRandomList(b *testing.B) {
+	var q list.List
+	rand.Seed(64738)
+	for i := 0; i < b.N; i++ {
+		if rand.Float32() < 0.8 {
+			q.PushBack(i)
+		}
+		if rand.Float32() < 0.8 {
+			q.PushFront(i)
+		}
+		if rand.Float32() < 0.5 {
+			if e := q.Front(); e != nil {
+				q.Remove(e)
+			}
+		}
+		if rand.Float32() < 0.5 {
+			if e := q.Back(); e != nil {
+				q.Remove(e)
+			}
+		}
+	}
+}