Last active
March 5, 2024 14:16
-
-
Save realFlowControl/14a4a9650bbe0776d81a552d77229fb1 to your computer and use it in GitHub Desktop.
1brc PHP v1
This file contains hidden or 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
<?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