blob: cb95b7fa1bc64d4fe10259d7be15db0f0f356940 [file] [log] [blame]
sslobodrd046be82019-01-16 10:02:22 -05001// Copyright 2014 Google Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// +build ignore
16
17// This binary compares memory usage between btree and gollrb.
18package main
19
20import (
21 "flag"
22 "fmt"
23 "math/rand"
24 "runtime"
25 "time"
26
27 "github.com/google/btree"
28 "github.com/petar/GoLLRB/llrb"
29)
30
31var (
32 size = flag.Int("size", 1000000, "size of the tree to build")
33 degree = flag.Int("degree", 8, "degree of btree")
34 gollrb = flag.Bool("llrb", false, "use llrb instead of btree")
35)
36
37func main() {
38 flag.Parse()
39 vals := rand.Perm(*size)
40 var t, v interface{}
41 v = vals
42 var stats runtime.MemStats
43 for i := 0; i < 10; i++ {
44 runtime.GC()
45 }
46 fmt.Println("-------- BEFORE ----------")
47 runtime.ReadMemStats(&stats)
48 fmt.Printf("%+v\n", stats)
49 start := time.Now()
50 if *gollrb {
51 tr := llrb.New()
52 for _, v := range vals {
53 tr.ReplaceOrInsert(llrb.Int(v))
54 }
55 t = tr // keep it around
56 } else {
57 tr := btree.New(*degree)
58 for _, v := range vals {
59 tr.ReplaceOrInsert(btree.Int(v))
60 }
61 t = tr // keep it around
62 }
63 fmt.Printf("%v inserts in %v\n", *size, time.Since(start))
64 fmt.Println("-------- AFTER ----------")
65 runtime.ReadMemStats(&stats)
66 fmt.Printf("%+v\n", stats)
67 for i := 0; i < 10; i++ {
68 runtime.GC()
69 }
70 fmt.Println("-------- AFTER GC ----------")
71 runtime.ReadMemStats(&stats)
72 fmt.Printf("%+v\n", stats)
73 if t == v {
74 fmt.Println("to make sure vals and tree aren't GC'd")
75 }
76}