-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
attempt 1: concurrently evaluate each city temperatures using goroutines
- Loading branch information
0 parents
commit 8bd5f43
Showing
30 changed files
with
30,365 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
measurements.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# 1BRC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/shraddhaag/1brc-go | ||
|
||
go 1.20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{Kunming=19.8/19.8/19.8} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Kunming;19.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.