Skip to content

Instantly share code, notes, and snippets.

@realFlowControl
Last active March 5, 2024 14:16
Show Gist options
  • Save realFlowControl/14a4a9650bbe0776d81a552d77229fb1 to your computer and use it in GitHub Desktop.
Save realFlowControl/14a4a9650bbe0776d81a552d77229fb1 to your computer and use it in GitHub Desktop.
1brc PHP v1
<?php
$stations = [];
$fp = fopen('measurements.txt', 'r');
while ($data = fgetcsv($fp, null, ';')) {
if (!isset($stations[$data[0]])) {
$stations[$data[0]] = [
$data[1],
$data[1],
$data[1],
1
];
} else {
$stations[$data[0]][3] ++;
$stations[$data[0]][2] += $data[1];
if ($data[1] < $stations[$data[0]][0]) {
$stations[$data[0]][0] = $data[1];
}
if ($data[1] > $stations[$data[0]][1]) {
$stations[$data[0]][1] = $data[1];
}
}
}
ksort($stations);
echo '{';
foreach($stations as $k=>&$station) {
$station[2] = $station[2]/$station[3];
echo $k, '=', $station[0], '/', $station[2], '/', $station[1], ',';
}
echo '}';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment