Skip to content

Commit

Permalink
attempt 2: do not sort float64 slice
Browse files Browse the repository at this point in the history
  • Loading branch information
shraddhaag committed Jan 16, 2024
1 parent 8bd5f43 commit 830e5df
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# 1BRC
# 1BRC

| Attempt Number | Approach | Execution Time | Diff | Commit |
|-----------------|---|---|---|--|
|1| Naive Implementation: Read temperatures into a map of cities. Iterate serially over each key (city) in map to find min, max and average temperatures.| 6:13.15 | ||
|2| Evaluate each city in map concurrently using goroutines.|4:32.80|-100.35||
|3|Remove sorting float64 slices. Calculate min, max and average by iterating.|4:25.59|-7.21||
19 changes: 11 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func main() {
}

func evaluate() string {
mapOfTemp, err := readFileLineByLineIntoAMap("./test_cases/measurements-1.txt")
mapOfTemp, err := readFileLineByLineIntoAMap("measurements.txt")
if err != nil {
panic(err)
}
Expand All @@ -69,21 +69,24 @@ func evaluate() string {
wg.Add(1)
go func(city string, temps []float64) {
defer wg.Done()
sort.Float64s(temps)

var avg float64
var min, max, avg float64
min, max = math.MaxFloat64, 0

for _, temp := range temps {
if temp < min {
min = temp
}

if temp > max {
max = temp
}
avg += temp
}

// fmt.Println(avg, len(temps))
avg = avg / float64(len(temps))
// fmt.Println(avg)
avg = math.Ceil(avg*10) / 10
// fmt.Println(avg)

updateResult(fmt.Sprintf("%s=%.1f/%.1f/%.1f", city, temps[0], avg, temps[len(temps)-1]))
updateResult(fmt.Sprintf("%s=%.1f/%.1f/%.1f", city, min, avg, max))

}(city, temps)
}
Expand Down
Binary file added profiles/cpu1.prof
Binary file not shown.
Binary file added profiles/mem1.prof
Binary file not shown.

0 comments on commit 830e5df

Please sign in to comment.