btree_mem.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // 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. // +build ignore
  15. // This binary compares memory usage between btree and gollrb.
  16. package main
  17. import (
  18. "flag"
  19. "fmt"
  20. "math/rand"
  21. "runtime"
  22. "time"
  23. "github.com/google/btree"
  24. "github.com/petar/GoLLRB/llrb"
  25. )
  26. var (
  27. size = flag.Int("size", 1000000, "size of the tree to build")
  28. degree = flag.Int("degree", 8, "degree of btree")
  29. gollrb = flag.Bool("llrb", false, "use llrb instead of btree")
  30. )
  31. func main() {
  32. flag.Parse()
  33. vals := rand.Perm(*size)
  34. var t, v interface{}
  35. v = vals
  36. var stats runtime.MemStats
  37. for i := 0; i < 10; i++ {
  38. runtime.GC()
  39. }
  40. fmt.Println("-------- BEFORE ----------")
  41. runtime.ReadMemStats(&stats)
  42. fmt.Printf("%+v\n", stats)
  43. start := time.Now()
  44. if *gollrb {
  45. tr := llrb.New()
  46. for _, v := range vals {
  47. tr.ReplaceOrInsert(llrb.Int(v))
  48. }
  49. t = tr // keep it around
  50. } else {
  51. tr := btree.New(*degree)
  52. for _, v := range vals {
  53. tr.ReplaceOrInsert(btree.Int(v))
  54. }
  55. t = tr // keep it around
  56. }
  57. fmt.Printf("%v inserts in %v\n", *size, time.Since(start))
  58. fmt.Println("-------- AFTER ----------")
  59. runtime.ReadMemStats(&stats)
  60. fmt.Printf("%+v\n", stats)
  61. for i := 0; i < 10; i++ {
  62. runtime.GC()
  63. }
  64. fmt.Println("-------- AFTER GC ----------")
  65. runtime.ReadMemStats(&stats)
  66. fmt.Printf("%+v\n", stats)
  67. if t == v {
  68. fmt.Println("to make sure vals and tree aren't GC'd")
  69. }
  70. }