Hamburg;12.0
Bulawayo;8.9
Palembang;38.8
Hamburg;34.2
St. John's;15.2
Cracow;12.6
...
{
Abha=-23.0/18.0/59.2,
Abidjan=-16.2/26.0/67.3,
Abéché=-10.0/29.4/69.0,
Accra=-10.1/26.4/66.4,
Addis Ababa=-23.7/16.0/67.0,
Adelaide=-27.8/17.3/58.5
...
}
$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 '}';
fgetcsv()
?fgets()
substr()
!
while ($data = fgets($fp, 108)) {
$pos = strpos($data, ';');
$city = substr($data, 0, $pos);
$temp = substr($data, $pos+1, -1);
// ...
while ($data = fgets($fp, 108)) {
$pos = strpos($data, ';');
$city = substr($data, 0, $pos);
$temp = substr($data, $pos+1, -1);
if (!isset($stations[$city])) {
$stations[$city] = [
$temp,
$temp,
$temp,
1
];
} else {
$stations[$city][3] ++;
$stations[$city][2] += $temp;
if ($temp < $stations[$city][0]) {
$stations[$city][0] = $temp;
}
if ($temp > $stations[$city][1]) {
$stations[$city][1] = $temp;
}
}
}
while ($data = fgets($fp, 108)) {
$pos = strpos($data, ';');
$city = substr($data, 0, $pos);
$temp = substr($data, $pos+1, -1);
if (!isset($stations[$city])) {
$stations[$city] = [
$temp,
$temp,
$temp,
1
];
} else {
$station = &$stations[$city];
$station[3] ++;
$station[2] += $temp;
if ($temp < $station[0]) {
$station[0] = $temp;
}
if ($temp > $station[1]) {
$station[1] = $temp;
}
}
}
if ($temp < $station[0]) {
$station[0] = $temp;
}
if ($temp > $station[1]) {
$station[1] = $temp;
}
if ($temp < $station[0]) {
$station[0] = $temp;
} elseif ($temp > $station[1]) {
$station[1] = $temp;
}
$a = "100";
$b = "25.5";
if ($a > $b) {
echo "100 > 25.5";
} else {
echo "25.5 > 100";
}
If both operands are numeric strings, or one operand is a number and the other one is a numeric string, then the comparison is done numerically.
while ($data = fgets($fp, 108)) {
$pos = strpos($data, ';');
$city = substr($data, 0, $pos);
$temp = (float)substr($data, $pos+1, -1);
if (!isset($stations[$city])) {
$stations[$city] = [
$temp,
$temp,
$temp,
1
];
} else {
$station = &$stations[$city];
$station[3] ++;
$station[2] += $temp;
if ($temp < $station[0]) {
$station[0] = $temp;
} elseif ($temp > $station[1]) {
$station[1] = $temp;
}
}
}
… and with no OPcache comes …
… no JIT!
opcache.enable_cli = 1; default: 0
opcache.jit-buffer-size = 10M; default: 0
Divide et impera
CFLAGS="-g -O0 ..."
CFLAGS="-Os ..."
if (!isset($stations[$city])) {
// ..
} else {
$station = &$stations[$city];
// ..
}
$station = &$stations[$city];
if ($station === NULL) {
// ..
} else {
// ..
}
while ($data = fgets($fp)) {
$chunk_start += strlen($data);
if ($chunk_start > $chunk_end) {
break;
}
// ..
while ($chunk_start < $chunk_end) {
$data = fgets($fp);
$chunk_start += strlen($data);
// ..
$data = fgets($fp);
$chunk_start += strlen($data);
$pos = strpos($data, ';');
$city = substr($data, 0, $pos);
$temp = (float)substr($data, $pos+1, -1);
$city = stream_get_line($fp, 99, ';');
$temp = stream_get_line($fp, 99, "\n");
$chunk_start += strlen($city) + strlen($temp) + 2;
$temp = (float)$temp;