/
r4.go
89 lines (79 loc) · 1.67 KB
/
r4.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
// r4: use fixed point int32s (*10) instead of float64s
//
// ~491ms for 10M rows (2.04x as fast as r1)
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"sort"
)
func r4(inputPath string, output io.Writer) error {
type stats struct {
min, max, count int32
sum int64
}
f, err := os.Open(inputPath)
if err != nil {
return err
}
defer f.Close()
stationStats := make(map[string]*stats)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Bytes()
station, tempBytes, hasSemi := bytes.Cut(line, []byte(";"))
if !hasSemi {
continue
}
negative := false
index := 0
if tempBytes[index] == '-' {
index++
negative = true
}
temp := int32(tempBytes[index] - '0')
index++
if tempBytes[index] != '.' {
temp = temp*10 + int32(tempBytes[index]-'0')
index++
}
index++ // skip '.'
temp = temp*10 + int32(tempBytes[index]-'0')
if negative {
temp = -temp
}
s := stationStats[string(station)]
if s == nil {
stationStats[string(station)] = &stats{
min: temp,
max: temp,
sum: int64(temp),
count: 1,
}
} else {
s.min = min(s.min, temp)
s.max = max(s.max, temp)
s.sum += int64(temp)
s.count++
}
}
stations := make([]string, 0, len(stationStats))
for station := range stationStats {
stations = append(stations, station)
}
sort.Strings(stations)
fmt.Fprint(output, "{")
for i, station := range stations {
if i > 0 {
fmt.Fprint(output, ", ")
}
s := stationStats[station]
mean := float64(s.sum) / float64(s.count) / 10
fmt.Fprintf(output, "%s=%.1f/%.1f/%.1f", station, float64(s.min)/10, mean, float64(s.max)/10)
}
fmt.Fprint(output, "}\n")
return nil
}