Skip to content

Commit 8bd5f43

Browse files
committed
attempt 1: concurrently evaluate each city temperatures using goroutines
0 parents  commit 8bd5f43

30 files changed

+30365
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
measurements.txt

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# 1BRC

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/shraddhaag/1brc-go
2+
3+
go 1.20

main.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"flag"
6+
"fmt"
7+
"log"
8+
"math"
9+
"os"
10+
"runtime"
11+
"runtime/pprof"
12+
"sort"
13+
"strconv"
14+
"strings"
15+
"sync"
16+
)
17+
18+
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
19+
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
20+
21+
func main() {
22+
23+
flag.Parse()
24+
if *cpuprofile != "" {
25+
f, err := os.Create("./profiles/" + *cpuprofile)
26+
if err != nil {
27+
log.Fatal("could not create CPU profile: ", err)
28+
}
29+
defer f.Close() // error handling omitted for example
30+
if err := pprof.StartCPUProfile(f); err != nil {
31+
log.Fatal("could not start CPU profile: ", err)
32+
}
33+
defer pprof.StopCPUProfile()
34+
}
35+
36+
fmt.Println(evaluate())
37+
38+
if *memprofile != "" {
39+
f, err := os.Create("./profiles/" + *memprofile)
40+
if err != nil {
41+
log.Fatal("could not create memory profile: ", err)
42+
}
43+
defer f.Close() // error handling omitted for example
44+
runtime.GC() // get up-to-date statistics
45+
if err := pprof.WriteHeapProfile(f); err != nil {
46+
log.Fatal("could not write memory profile: ", err)
47+
}
48+
}
49+
}
50+
51+
func evaluate() string {
52+
mapOfTemp, err := readFileLineByLineIntoAMap("./test_cases/measurements-1.txt")
53+
if err != nil {
54+
panic(err)
55+
}
56+
57+
var result []string
58+
var wg sync.WaitGroup
59+
var mx sync.Mutex
60+
61+
updateResult := func(input string) {
62+
mx.Lock()
63+
defer mx.Unlock()
64+
65+
result = append(result, input)
66+
}
67+
68+
for city, temps := range mapOfTemp {
69+
wg.Add(1)
70+
go func(city string, temps []float64) {
71+
defer wg.Done()
72+
sort.Float64s(temps)
73+
74+
var avg float64
75+
76+
for _, temp := range temps {
77+
avg += temp
78+
}
79+
80+
// fmt.Println(avg, len(temps))
81+
avg = avg / float64(len(temps))
82+
// fmt.Println(avg)
83+
avg = math.Ceil(avg*10) / 10
84+
// fmt.Println(avg)
85+
86+
updateResult(fmt.Sprintf("%s=%.1f/%.1f/%.1f", city, temps[0], avg, temps[len(temps)-1]))
87+
88+
}(city, temps)
89+
}
90+
91+
wg.Wait()
92+
sort.Strings(result)
93+
return strings.Join(result, ", ")
94+
}
95+
96+
func readFileLineByLineIntoAMap(filepath string) (map[string][]float64, error) {
97+
mapOfTemp := make(map[string][]float64)
98+
99+
file, err := os.Open(filepath)
100+
if err != nil {
101+
return nil, err
102+
}
103+
104+
scanner := bufio.NewScanner(file)
105+
106+
for scanner.Scan() {
107+
text := scanner.Text()
108+
109+
index := strings.Index(text, ";")
110+
city := text[:index]
111+
temp := convertStringToFloat(text[index+1:])
112+
113+
if _, ok := mapOfTemp[city]; ok {
114+
mapOfTemp[city] = append(mapOfTemp[city], temp)
115+
} else {
116+
mapOfTemp[city] = []float64{temp}
117+
}
118+
}
119+
120+
return mapOfTemp, nil
121+
}
122+
123+
func convertStringToFloat(input string) float64 {
124+
output, _ := strconv.ParseFloat(input, 64)
125+
return output
126+
}

profiles/cpu.prof

27.2 KB
Binary file not shown.

profiles/mem.prof

2.04 KB
Binary file not shown.

test_cases/measurements-1.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{Kunming=19.8/19.8/19.8}

test_cases/measurements-1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Kunming;19.8

test_cases/measurements-10.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{Adelaide=15.0/15.0/15.0, Cabo San Lucas=14.9/14.9/14.9, Dodoma=22.2/22.2/22.2, Halifax=12.9/12.9/12.9, Karachi=15.4/15.4/15.4, Pittsburgh=9.7/9.7/9.7, Ségou=25.7/25.7/25.7, Tauranga=38.2/38.2/38.2, Xi'an=24.2/24.2/24.2, Zagreb=12.2/12.2/12.2}

test_cases/measurements-10.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Halifax;12.9
2+
Zagreb;12.2
3+
Cabo San Lucas;14.9
4+
Adelaide;15.0
5+
Ségou;25.7
6+
Pittsburgh;9.7
7+
Karachi;15.4
8+
Xi'an;24.2
9+
Dodoma;22.2
10+
Tauranga;38.2

0 commit comments

Comments
 (0)