Skip to content

kelvinandreas/goroutine-benchmark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Concurrent Weather Station Aggregator

A Go program that reads temperature readings from a CSV file and computes aggregate statistics (sum, average, min, max) using concurrent goroutines.

Problem

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.

Approach

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.

How It Works (Optimized)

  1. Read all lines from the CSV into memory.
  2. Divide the lines into N equal chunks (where N = number of CPU cores).
  3. Each goroutine processes its chunk independently — no shared state, no locks.
  4. Results from all goroutines are collected via a buffered channel.
  5. A final merge step combines partial stats into the overall result.

Running

go run .

Make sure weather_stations.csv is present in the same directory.

Benchmark Results

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages