A Go program that reads temperature readings from a CSV file and computes aggregate statistics (sum, average, min, max) using concurrent goroutines.
Given a large CSV file where each line is:
StationName;Temperature
Example:
Tokyo;35.6897
Jakarta;-6.1750
Delhi;28.6100
Compute the sum, average, min, and max temperature across all readings as fast as possible.
Two implementations are provided:
| File | Strategy |
|---|---|
naive.go |
Single-pass sequential scan |
opt.go |
Splits data into chunks, processes each chunk in a goroutine, then merges results |
The optimized version uses runtime.NumCPU() to determine how many worker goroutines to spawn, keeping CPU usage balanced.
- Read all lines from the CSV into memory.
- Divide the lines into
Nequal chunks (where N = number of CPU cores). - Each goroutine processes its chunk independently — no shared state, no locks.
- Results from all goroutines are collected via a buffered channel.
- A final merge step combines partial stats into the overall result.
go run .Make sure weather_stations.csv is present in the same directory.
Tested on 1,000,000 rows of weather station data:
=== Naive (sequential) ===
Count: 1000000
Sum: 25739888.4640
Average: 25.7399
Min: -54.9333
Max: 81.7166
Elapsed: 44.133006ms
=== Optimized (goroutines) ===
Count: 1000000
Sum: 25739888.4640
Average: 25.7399
Min: -54.9333
Max: 81.7166
Elapsed: 14.693871ms
--- Comparison ---
Speedup: 3.00x faster with goroutines
The goroutine-based version is ~3x faster on 1M rows. The gap grows wider as data size increases, since each goroutine works on an independent chunk with zero locking overhead.