-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.go
More file actions
128 lines (110 loc) · 3.04 KB
/
Copy pathtrie.go
File metadata and controls
128 lines (110 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"time"
"github.com/dghubble/trie"
)
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
// PrintMemUsage outputs the current, total and OS memory being used. As well as the number
// of garage collection cycles completed.
func PrintMemUsage(memprofile string) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
if memprofile != "" {
f, err := os.Create(memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
f.Close()
}
}
var totalBytes int64
func main() {
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
var trieApproach = flag.Bool("trie", false, "use trie approach")
var mapApproach = flag.Bool("map", false, "use map approach")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
dir, err := os.Getwd()
if err != nil {
log.Fatal("Error can get cwd")
}
count := 0
if *trieApproach {
/* Trie approach */
pathTrie := trie.NewPathTrie()
count = 0
walkFn := func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
// fmt.Println(path)
pathTrie.Put(path, info)
count++
}
return nil
}
timeStart := time.Now()
filepath.Walk(dir, walkFn)
fmt.Println("Trie: Time adding", count, "files:", time.Now().Sub(timeStart))
trieWalkFn := func(key string, value interface{}) error {
//fmt.Println(key, value)
//fmt.Println(key)
totalBytes += value.(os.FileInfo).Size()
return nil
}
PrintMemUsage(*memprofile)
totalBytes = 0
timeStart = time.Now()
pathTrie.Walk(trieWalkFn)
fmt.Println("Trie: total bytes", totalBytes, "Time getting files:", time.Now().Sub(timeStart))
}
if *mapApproach {
/* Map approach */
fileInfos := make(map[string]os.FileInfo)
count = 0
walkFn := func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
// fmt.Println(path)
fileInfos[path] = info
count++
}
return nil
}
timeStart := time.Now()
filepath.Walk(dir, walkFn)
fmt.Println("Map: Time adding", count, "files:", time.Now().Sub(timeStart))
PrintMemUsage(*memprofile)
totalBytes = 0
timeStart = time.Now()
for _, fileInfo := range fileInfos {
totalBytes += fileInfo.Size()
}
fmt.Println("Map: total bytes", totalBytes, "Time getting files:", time.Now().Sub(timeStart))
}
}