Skip to content

Commit

Permalink
attempt 1: concurrently evaluate each city temperatures using goroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
shraddhaag committed Jan 16, 2024
0 parents commit 8bd5f43
Show file tree
Hide file tree
Showing 30 changed files with 30,365 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
measurements.txt
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 1BRC
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/shraddhaag/1brc-go

go 1.20
126 changes: 126 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package main

import (
"bufio"
"flag"
"fmt"
"log"
"math"
"os"
"runtime"
"runtime/pprof"
"sort"
"strconv"
"strings"
"sync"
)

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")

func main() {

flag.Parse()
if *cpuprofile != "" {
f, err := os.Create("./profiles/" + *cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close() // error handling omitted for example
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}

fmt.Println(evaluate())

if *memprofile != "" {
f, err := os.Create("./profiles/" + *memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer f.Close() // error handling omitted for example
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
}

func evaluate() string {
mapOfTemp, err := readFileLineByLineIntoAMap("./test_cases/measurements-1.txt")
if err != nil {
panic(err)
}

var result []string
var wg sync.WaitGroup
var mx sync.Mutex

updateResult := func(input string) {
mx.Lock()
defer mx.Unlock()

result = append(result, input)
}

for city, temps := range mapOfTemp {
wg.Add(1)
go func(city string, temps []float64) {
defer wg.Done()
sort.Float64s(temps)

var avg float64

for _, temp := range temps {
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]))

}(city, temps)
}

wg.Wait()
sort.Strings(result)
return strings.Join(result, ", ")
}

func readFileLineByLineIntoAMap(filepath string) (map[string][]float64, error) {
mapOfTemp := make(map[string][]float64)

file, err := os.Open(filepath)
if err != nil {
return nil, err
}

scanner := bufio.NewScanner(file)

for scanner.Scan() {
text := scanner.Text()

index := strings.Index(text, ";")
city := text[:index]
temp := convertStringToFloat(text[index+1:])

if _, ok := mapOfTemp[city]; ok {
mapOfTemp[city] = append(mapOfTemp[city], temp)
} else {
mapOfTemp[city] = []float64{temp}
}
}

return mapOfTemp, nil
}

func convertStringToFloat(input string) float64 {
output, _ := strconv.ParseFloat(input, 64)
return output
}
Binary file added profiles/cpu.prof
Binary file not shown.
Binary file added profiles/mem.prof
Binary file not shown.
1 change: 1 addition & 0 deletions test_cases/measurements-1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{Kunming=19.8/19.8/19.8}
1 change: 1 addition & 0 deletions test_cases/measurements-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Kunming;19.8
1 change: 1 addition & 0 deletions test_cases/measurements-10.out
Original file line number Diff line number Diff line change
@@ -0,0 +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}
10 changes: 10 additions & 0 deletions test_cases/measurements-10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Halifax;12.9
Zagreb;12.2
Cabo San Lucas;14.9
Adelaide;15.0
Ségou;25.7
Pittsburgh;9.7
Karachi;15.4
Xi'an;24.2
Dodoma;22.2
Tauranga;38.2
1 change: 1 addition & 0 deletions test_cases/measurements-10000-unique-keys.out

Large diffs are not rendered by default.

Loading

0 comments on commit 8bd5f43

Please sign in to comment.