非常好的GetRange实现

This commit is contained in:
huangsimin
2019-03-20 19:20:06 +08:00
parent cc2f390743
commit 7dcde4cb97
4 changed files with 594 additions and 159 deletions

View File

@@ -1,12 +1,64 @@
package plist
import (
"bytes"
"encoding/gob"
"io/ioutil"
"log"
"os"
"testing"
"github.com/Pallinder/go-randomdata"
"github.com/emirpasic/gods/utils"
)
const CompartorSize = 100
const NumberMax = 50000000
func Save(t *testing.T) {
f, err := os.OpenFile("../l.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
log.Println(err)
}
//fmt.Println(userBytes)
var l []int
// for i := 0; len(l) < 1000; i++ {
// v := randomdata.Number(0, 65535)
// l = append(l, v)
// }
//m := make(map[int]int)
for i := 0; len(l) < CompartorSize; i++ {
v := randomdata.Number(0, NumberMax)
// if _, ok := m[v]; !ok {
// m[v] = v
l = append(l, v)
// }
}
var result bytes.Buffer
encoder := gob.NewEncoder(&result)
encoder.Encode(l)
lbytes := result.Bytes()
f.Write(lbytes)
}
func loadTestData() []int {
data, err := ioutil.ReadFile("../l.log")
if err != nil {
log.Println(err)
}
var l []int
decoder := gob.NewDecoder(bytes.NewReader(data))
decoder.Decode(&l)
return l
}
func TestInsert(t *testing.T) {
pl := New(utils.IntComparator)
for i := 0; i < 10; i++ {
@@ -148,10 +200,10 @@ func TestRemove(t *testing.T) {
}
func BenchmarkInsert(b *testing.B) {
func BenchmarkGet(b *testing.B) {
pl := New(utils.IntComparator)
b.N = 3000
b.N = 100
for i := 0; i < b.N; i++ {
v := randomdata.Number(0, 65535)
@@ -160,6 +212,7 @@ func BenchmarkInsert(b *testing.B) {
b.ResetTimer()
b.StartTimer()
for i := 0; i < b.N; i++ {
if i%2 == 0 {
pl.Get(i)
@@ -167,3 +220,19 @@ func BenchmarkInsert(b *testing.B) {
}
}
func BenchmarkInsert(b *testing.B) {
l := loadTestData()
b.ResetTimer()
b.StartTimer()
execCount := 500
b.N = len(l) * execCount
for i := 0; i < execCount; i++ {
pl := New(utils.IntComparator)
for _, v := range l {
pl.Push(v)
}
}
}