-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.php
51 lines (44 loc) · 1.17 KB
/
run.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
// https://dev.to/realflowcontrol/processing-one-billion-rows-in-php-3eg0
error_reporting(E_ERROR | E_PARSE);
$start = microtime(true);
$sep = ",";
$stations = [];
//$fp = fopen('measurements.1e6.csv', 'r');
//$fp = fopen('measurements.1e7.csv', 'r');
$fp = fopen('measurements.1e9.csv', 'r');
$first = fgetcsv($fp, null, ',');
while ($data = fgets($fp)) {
$pos = strpos($data, $sep);
$temp = (float)substr($data, 0, $pos);
$city = substr($data, $pos+1, -1);
$station = &$stations[$city];
if ($station == NULL) {
$station = [
$temp,
$temp,
$temp,
1
];
}
$station[3] ++;
$station[2] += $temp;
if ($temp < $station[0]) {
$station[0] = $temp;
}
elseif ($temp > $station[1]) {
$station[1] = $temp;
}
}
ksort($stations);
// echo count($stations);
// print_r($stations);
echo '{';
foreach($stations as $k=>&$station) {
$station[2] = $station[2]/$station[3];
echo $k, '=', $station[0], '/', $station[2], '/', $station[1], ', ';
}
echo '}';
$end = microtime(true);
$time = number_format(($end - $start), 2);
echo "\nEnded in ", $time, " seconds\n";