-
Notifications
You must be signed in to change notification settings - Fork 4
/
optimized.go
102 lines (87 loc) · 2.14 KB
/
optimized.go
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
// Run with:
// go build optimized.go
// sudo sysctl vm.drop_caches=3; ./optimized <kjvbible_x100.txt >/dev/null
package main
import (
"fmt"
"io"
"os"
"sort"
"time"
)
func main() {
tStart := time.Now()
dRead := time.Duration(0)
var word []byte
buf := make([]byte, 64*1024)
counts := make(map[string]*int)
for {
// Read input in 64KB blocks till EOF.
tRead1 := time.Now()
n, err := os.Stdin.Read(buf)
tRead2 := time.Now()
dRead += tRead2.Sub(tRead1)
if err != nil && err != io.EOF {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if n == 0 {
break
}
// Count words in the buffer.
for i := 0; i < n; i++ {
c := buf[i]
// Found a whitespace char, count last word.
if c <= ' ' {
if len(word) > 0 {
increment(counts, word)
word = word[:0] // reset word buffer
}
continue
}
// Convert to ASCII lowercase as we go.
if c >= 'A' && c <= 'Z' {
c = c + ('a' - 'A')
}
// Add non-space char to word buffer.
word = append(word, c)
}
}
// Count last word, if any.
if len(word) > 0 {
increment(counts, word)
}
tProcess := time.Now()
// Convert to slice of Count, sort by count descending, print.
ordered := make([]Count, 0, len(counts))
for word, count := range counts {
ordered = append(ordered, Count{word, *count})
}
sort.Slice(ordered, func(i, j int) bool {
return ordered[i].Count > ordered[j].Count
})
tSort := time.Now()
for _, count := range ordered {
fmt.Println(count.Word, count.Count)
}
tEnd := time.Now()
fmt.Fprintf(os.Stderr, "Reading : %v\n", dRead.Seconds())
fmt.Fprintf(os.Stderr, "Processing: %v\n", (tProcess.Sub(tStart) - dRead).Seconds())
fmt.Fprintf(os.Stderr, "Sorting : %v\n", tSort.Sub(tProcess).Seconds())
fmt.Fprintf(os.Stderr, "Outputting: %v\n", tEnd.Sub(tSort).Seconds())
fmt.Fprintf(os.Stderr, "TOTAL : %v\n", tEnd.Sub(tStart).Seconds())
}
func increment(counts map[string]*int, word []byte) {
if p, ok := counts[string(word)]; ok {
// Word already in map, increment existing int via pointer.
*p++
return
}
// Word not in map, insert new int.
n := 1
counts[string(word)] = &n
}
type Count struct {
Word string
Count int
}