From b324ea202f31418ebc9bfd6d0929ec412ce72cd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B0=E8=B6=8A=E6=B4=8B?= Date: Mon, 8 Feb 2021 15:20:20 +0800 Subject: [PATCH 1/3] test branch --- go.mod | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 go.mod diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ec78f66 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module sortedset/m + +go 1.15 From dac5c4c6ee67d323bff6e440cd3017cd47bb64eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B0=E8=B6=8A=E6=B4=8B?= Date: Mon, 8 Feb 2021 15:31:30 +0800 Subject: [PATCH 2/3] add test case for cost --- sortedset_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/sortedset_test.go b/sortedset_test.go index 233292c..8eb640b 100644 --- a/sortedset_test.go +++ b/sortedset_test.go @@ -1,7 +1,10 @@ package sortedset import ( + "fmt" + "math/rand" "testing" + "time" ) func checkOrder(t *testing.T, nodes []*SortedSetNode, expectedOrder []string) { @@ -162,3 +165,35 @@ func TestCase2(t *testing.T) { nodes = sortedset.GetByScoreRange(500, -500, nil) checkOrder(t, nodes, []string{"g", "f", "e", "a", "h"}) } +func TestCase3(t *testing.T) { + sortedset := New() + testSet := make(map[string]int64) + rand.Seed(0) + for len(testSet) < 1000000 { + k := rand.Int63() + ks := fmt.Sprintf("%d", k) + if _, ok := testSet[ks]; !ok { + testSet[ks] = k + } + } + + startts := time.Now() + + for k, v := range testSet { + sortedset.AddOrUpdate(k, SCORE(v), v) + } + + dur := time.Since(startts) + t.Logf("insert cost %v ", dur) + + startts = time.Now() + for { + min := sortedset.PopMin() + if min == nil { + break + } + } + dur = time.Since(startts) + t.Logf("pop cost %v ", dur) + +} From 85be0830accc80e5347b2d5a6f069f1623cc86b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B0=E8=B6=8A=E6=B4=8B?= Date: Tue, 9 Feb 2021 17:54:54 +0800 Subject: [PATCH 3/3] add print func --- sortedset.go | 6 ++++++ sortedset_test.go | 18 +++++++++++++----- sortedsetnode.go | 16 ++++++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/sortedset.go b/sortedset.go index 9220857..214d317 100644 --- a/sortedset.go +++ b/sortedset.go @@ -41,6 +41,11 @@ type SortedSet struct { dict map[string]*SortedSetNode } +func (this *SortedSet) Print() { + if this.header != nil { + this.header.Print() + } +} func createNode(level int, score SCORE, key string, value interface{}) *SortedSetNode { node := SortedSetNode{ score: score, @@ -96,6 +101,7 @@ func (this *SortedSet) insertNode(score SCORE, key string, value interface{}) *S * if the element is already inside or not. */ level := randomLevel() + //level := this.level + 1 if level > this.level { // add a new level for i := this.level; i < level; i++ { rank[i] = 0 diff --git a/sortedset_test.go b/sortedset_test.go index 8eb640b..90a713f 100644 --- a/sortedset_test.go +++ b/sortedset_test.go @@ -23,16 +23,23 @@ func TestCase1(t *testing.T) { sortedset := New() sortedset.AddOrUpdate("a", 89, "Kelly") + sortedset.Print() sortedset.AddOrUpdate("b", 100, "Staley") + sortedset.Print() sortedset.AddOrUpdate("c", 100, "Jordon") + sortedset.Print() sortedset.AddOrUpdate("d", -321, "Park") + sortedset.Print() sortedset.AddOrUpdate("e", 101, "Albert") + sortedset.Print() sortedset.AddOrUpdate("f", 99, "Lyman") + sortedset.Print() sortedset.AddOrUpdate("g", 99, "Singleton") + sortedset.Print() sortedset.AddOrUpdate("h", 70, "Audrey") - + sortedset.Print() sortedset.AddOrUpdate("e", 99, "ntrnrt") - + sortedset.Print() sortedset.Remove("b") node := sortedset.GetByRank(3, false) @@ -169,7 +176,8 @@ func TestCase3(t *testing.T) { sortedset := New() testSet := make(map[string]int64) rand.Seed(0) - for len(testSet) < 1000000 { + count := 100000 + for len(testSet) < count { k := rand.Int63() ks := fmt.Sprintf("%d", k) if _, ok := testSet[ks]; !ok { @@ -184,7 +192,7 @@ func TestCase3(t *testing.T) { } dur := time.Since(startts) - t.Logf("insert cost %v ", dur) + t.Logf("insert cost %v ", dur/time.Duration(count)) startts = time.Now() for { @@ -194,6 +202,6 @@ func TestCase3(t *testing.T) { } } dur = time.Since(startts) - t.Logf("pop cost %v ", dur) + t.Logf("pop cost %v ", dur/time.Duration(count)) } diff --git a/sortedsetnode.go b/sortedsetnode.go index b060191..1ddad8c 100644 --- a/sortedsetnode.go +++ b/sortedsetnode.go @@ -24,6 +24,8 @@ package sortedset +import "fmt" + type SortedSetLevel struct { forward *SortedSetNode span int64 @@ -47,3 +49,17 @@ func (this *SortedSetNode) Key() string { func (this *SortedSetNode) Score() SCORE { return this.score } +func (this *SortedSetNode) Print() { + for i, _ := range this.level { + x := this.level[i].forward + if x == nil { + continue + } + fmt.Printf("No %d", i) + for x != nil { + fmt.Printf(" %d:%p:[%d]->", x.score, x, x.level[i].span) + x = x.level[i].forward + } + fmt.Printf("nil\n") + } +}