3v4l.org

run code in 300+ PHP versions simultaneously
<?php // тестовое окружение не перезагружает строковые функции!!! /* ЗАДАЧА 1 * * Дано: * - Текст из *.csv файла * Необходимо: * 1. Распарсить текст, подготовить данные к работе (элемент = тип Объект) * 2. Отсортировать данные по дате затем КБК и вывести в таблице, таким образом, что если существует несколько записей на одну дату с одним КБК, то в поле %% считать среднее, а в скобках вывести кол-во елементов. * * Пример Табл.: * | ДАТА | КБК | Адрес | %% | * | 11.01.2013 | 1-01-001 | Спб, Восстания, 1 | 84% (2) | * */ $data = " 02-01-2013;1-01-001;Спб, Восстания, 1;95 05-01-2013;1-02-011;Спб, Савушкина, 106;87 01-01-2013;1-01-003;Спб, Обводный канал, 12 ;92 06-02-2013;2-05-245;Ростов-на-Дону, Стачек, 41;79 12-01-2012;5-10-002;Новосибирск, Ленина, 105;75 01-01-2013;1-01-003;Спб, Обводный канал, 12 ;98 03-01-2013;6-30-855;Сочи, Коммунистическая, 2;84 05-01-2013;2-04-015;Ростов-на-Дону, Пушкинская, 102;71 07-01-2013;6-01-010;Сочи, Приморская, 26;62 05-01-2013;1-02-011;Спб, Савушкина, 106;89 01-01-2013;1-01-003;Спб, Обводный канал, 12 ;57 "; class Element { public $date; public $kbk; public $addres; public $oth; public $kbkNum; public function __construct($csvtxt) { if ($csvtxt) { $ar=str_getcsv($csvtxt, ';'); if (sizeof($ar)==4) { if ($dt=DateTime::createFromFormat('d-m-Y', $ar[0])) { $this->date=$dt->getTimeStamp(); $this->kbk=trim($ar[1]); $this->kbkNum=intval(str_replace('-', '', $this->kbk)); $this->addres=trim($ar[2]); $this->oth=$ar[3]; } else { throw new \Exception('Некорректная дата.'); } } else { throw new \Exception('Некорректная строка.'); } } } public function getKbkNum() { return $this->kbkNum; } public function getDate() { return $this->date; } public function getKbk() { return $this->kbk; } public function getAddres() { return $this->addres; } public function getOth() { return $this->oth; } public function compare(Element $e) { // сравнение элментов if ($this->getDate()>$e->getDate()) { return 1; } elseif ($this->getDate()<$e->getDate()) { return -1; } else { // даты равны - сравнение кбк if ($this->getKbkNum()==$e->getKbkNum()) return 0;// равны if ($this->getKbkNum()>$e->getKbkNum()) return 1;// больше if ($this->getKbkNum()<$e->getKbkNum()) return -1;// меньше } } // end compare } // end Element class Elements { protected $ar=array(); protected $maxSize=array('addrres'=>5, 'kbk'=>3); public function __construct($text) { $p=0; $size=strlen($text); do { $f=strpos($text, "\n", $p); if ($f!==false) { if ($f>0 && substr($text, $f-1, 1)=="\r") { $line=substr($text, $p, $f-1-$p); } else { $line=substr($text, $p, $f-$p); } $p=$f+1; } elseif ($p<$size) { $line=substr($text, $p); $p=false; } if ($line) { try { $e=new Element($line); $this->maxSize['addres']=max($this->maxSize['addres'], strlen($e->getAddres())); $this->maxSize['kbk']=max($this->maxSize['kbk'], strlen($e->getKbk())); $this->ar[]=$e; } catch (\Exception $e) {} // ошибка формата } } while ($p!==false && $p<$size); } // end public function count() { return sizeof($this->ar); } public function sort() { if (sizeof($this->ar)>0) { usort($this->ar, function ($a, $b) { return $a->compare($b); }); } } // end sort public function __toString() { if (sizeof($this->ar)>0) { $printMask="| %-10s | %-".$this->maxSize['kbk']."s | %-".$this->maxSize['addres']."s | %-8s |\n"; $s=vsprintf($printMask, array('ДАТА', 'КБК', 'АДРЕС', '%%')); $counter=0; $sum=0; $print=false; $prev=false; $count=sizeof($this->ar); for ($i=0; $i<$count; $i++) { $next=( $i+1<$count ? $this->ar[$i+1] : false); $curr=$this->ar[$i]; $counter++; $sum+=$curr->getOth(); if ($next) { if ($curr->getKbkNum()!=$next->getKbkNum() || $curr->getDate()!=$next->getDate()) { $print=true; } } else { // последняя строка данных $print=true; } if ($print) { $forPrint=array( date('d-m-Y', $curr->getDate()), $curr->getKbk(), $curr->getAddres() ); if ($counter>1) { $forPrint[]=round($sum/$counter, 0).'% ('.$counter.')'; } else { $forPrint[]=$sum.'%'; } $s.=vsprintf($printMask, $forPrint); $counter=0; $sum=0; $print=false; } } // end for return $s; } return ''; } // end toString } // end Elements $c=new Elements($data); $c->sort(); echo $c;

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0090.01218.50
8.3.50.0070.01617.93
8.3.40.0160.00019.21
8.3.30.0060.01219.00
8.3.20.0030.00619.23
8.3.10.0040.00421.89
8.3.00.0050.00321.01
8.2.180.0090.00918.79
8.2.170.0120.00322.96
8.2.160.0070.00720.34
8.2.150.0070.01125.66
8.2.140.0050.00324.66
8.2.130.0080.00026.16
8.2.120.0040.00420.02
8.2.110.0000.00922.25
8.2.100.0060.00618.22
8.2.90.0040.00418.13
8.2.80.0030.00618.29
8.2.70.0030.00618.28
8.2.60.0060.00318.41
8.2.50.0030.00518.13
8.2.40.0000.00920.52
8.2.30.0050.00519.45
8.2.20.0040.00418.27
8.2.10.0000.00818.25
8.2.00.0000.00818.08
8.1.280.0080.00825.92
8.1.270.0030.00522.31
8.1.260.0000.00826.35
8.1.250.0080.00028.09
8.1.240.0030.00620.89
8.1.230.0030.00920.91
8.1.220.0030.00717.79
8.1.210.0090.00018.77
8.1.200.0040.00717.73
8.1.190.0000.00817.60
8.1.180.0060.00318.10
8.1.170.0040.00418.77
8.1.160.0000.00719.18
8.1.150.0070.00020.42
8.1.140.0030.00619.84
8.1.130.0000.00717.88
8.1.120.0000.00717.68
8.1.110.0060.00317.69
8.1.100.0080.00017.68
8.1.90.0060.00317.75
8.1.80.0030.00617.74
8.1.70.0070.00017.63
8.1.60.0040.00417.70
8.1.50.0080.00017.60
8.1.40.0030.00717.70
8.1.30.0030.00617.93
8.1.20.0040.00417.91
8.1.10.0000.00817.80
8.1.00.0060.00317.83
8.0.300.0040.00421.63
8.0.290.0060.00317.13
8.0.280.0000.00718.75
8.0.270.0040.00417.43
8.0.260.0000.00817.52
8.0.250.0080.00017.30
8.0.240.0040.00417.39
8.0.230.0040.00417.28
8.0.220.0040.00417.29
8.0.210.0040.00417.33
8.0.200.0000.00717.28
8.0.190.0000.00717.24
8.0.180.0030.00517.36
8.0.170.0000.00817.35
8.0.160.0000.00917.27
8.0.150.0040.00417.12
8.0.140.0060.00317.23
8.0.130.0000.00713.64
8.0.120.0030.00517.22
8.0.110.0000.00817.30
8.0.100.0050.00317.19
8.0.90.0040.00417.19
8.0.80.0060.00917.31
8.0.70.0050.00317.12
8.0.60.0050.00317.12
8.0.50.0040.00417.27
8.0.30.0080.00917.17
8.0.20.0120.00917.41
8.0.10.0040.00417.31
8.0.00.0090.00917.23
7.4.330.0030.00715.55
7.4.320.0000.00616.88
7.4.300.0000.00616.79
7.4.290.0000.00716.83
7.4.280.0070.00316.82
7.4.270.0030.00716.80
7.4.260.0030.00716.81
7.4.250.0000.00716.74
7.4.240.0030.00516.84
7.4.230.0040.00416.82
7.4.220.0160.00016.80
7.4.210.0130.00316.72
7.4.200.0040.00416.84
7.4.160.0120.00816.89
7.4.150.0190.00517.40
7.4.140.0140.00917.86
7.4.130.0110.00816.83
7.4.120.0040.01416.95
7.4.110.0170.00316.75
7.4.100.0100.01317.07
7.4.90.0160.00316.95
7.4.80.0170.00019.39
7.4.70.0060.01017.02
7.4.60.0110.00616.80
7.4.50.0100.00016.58
7.4.40.0250.01616.77
7.4.30.0060.01016.81
7.4.00.0000.01515.08
7.3.330.0030.00313.62
7.3.320.0000.00713.59
7.3.310.0000.00716.49
7.3.300.0000.00716.45
7.3.290.0100.00516.63
7.3.280.0080.01016.66
7.3.270.0090.01217.40
7.3.260.0130.00516.88
7.3.250.0120.00916.76
7.3.240.0140.00716.85
7.3.230.0110.00716.85
7.3.210.0100.00716.84
7.3.200.0060.01216.75
7.3.190.0060.01216.71
7.3.180.0070.01616.86
7.3.170.0030.01416.84
7.3.160.0120.00616.81
7.3.120.0070.01114.99
7.3.110.0030.00714.97
7.3.100.0030.01515.14
7.3.90.0030.01415.12
7.3.80.0090.00615.22
7.3.70.0080.01214.99
7.3.60.0040.01214.81
7.3.50.0030.00614.98
7.3.40.0040.00815.07
7.3.30.0030.01014.82
7.3.20.0070.00716.84
7.3.10.0120.00316.71
7.3.00.0040.01116.77
7.2.330.0120.00616.76
7.2.320.0120.00616.77
7.2.310.0130.00417.03
7.2.300.0150.00416.93
7.2.290.0100.01017.20
7.2.250.0060.01215.15
7.2.240.0090.00915.42
7.2.230.0030.01515.14
7.2.220.0060.00915.11
7.2.210.0030.00915.58
7.2.200.0110.00415.27
7.2.190.0040.01115.24
7.2.180.0060.00615.32
7.2.170.0040.01215.27
7.2.160.0030.01515.18
7.2.150.0040.01516.66
7.2.140.0090.00617.15
7.2.130.0000.01016.81
7.2.120.0120.00317.00
7.2.110.0060.00616.87
7.2.100.0100.00717.04
7.2.90.0080.00616.91
7.2.80.0040.01517.17
7.2.70.0100.00316.99
7.2.60.0070.01017.13
7.2.50.0090.00716.84
7.2.40.0080.00816.87
7.2.30.0000.01716.93
7.2.20.0100.00617.06
7.2.10.0030.01317.07
7.2.00.0030.01017.14
7.1.330.0100.00715.84
7.1.320.0100.00615.89
7.1.310.0000.01415.65
7.1.300.0000.01015.69
7.1.290.0000.01415.62
7.1.280.0030.01015.76
7.1.270.0090.00915.98
7.1.260.0000.01215.95
7.1.250.0060.00615.86
7.1.240.0030.01316.02
7.1.230.0030.01015.96
7.1.220.0060.00615.87
7.1.210.0030.00715.48
7.1.200.0030.01215.86
7.1.190.0070.01015.95
7.1.180.0120.00315.68
7.1.170.0060.01016.00
7.1.160.0030.01015.73
7.1.150.0070.00715.99
7.1.140.0040.01415.78
7.1.130.0000.01315.76
7.1.120.0080.00815.89
7.1.110.0070.00715.89
7.1.100.0070.01116.02
7.1.90.0130.00316.05
7.1.80.0120.00316.17
7.1.70.0060.00416.37
7.1.60.0050.00716.40
7.1.50.0040.01116.01
7.1.40.0000.01015.95
7.1.30.0040.00816.00
7.1.20.0070.00715.48
7.1.10.0030.01016.09
7.1.00.0020.04419.22
7.0.330.0060.00615.70
7.0.320.0130.00315.56
7.0.310.0000.01515.77
7.0.300.0050.00515.40
7.0.290.0030.00915.41
7.0.280.0030.01015.66
7.0.270.0030.00915.70
7.0.260.0000.01215.57
7.0.250.0000.00915.54
7.0.240.0000.01515.55
7.0.230.0100.00615.68
7.0.220.0080.00815.46
7.0.210.0070.00715.32
7.0.200.0040.00616.20
7.0.190.0100.00315.29
7.0.180.0000.00915.40
7.0.170.0000.01315.57
7.0.160.0090.00615.33
7.0.150.0100.00315.43
7.0.140.0080.00415.48
7.0.130.0050.04118.64
7.0.120.0130.02318.76
7.0.110.0020.03718.67
7.0.100.0050.03418.87
7.0.90.0070.03418.79
7.0.80.0100.03018.71
7.0.70.0070.03218.72
7.0.60.0100.02918.72
7.0.50.0080.03018.74
7.0.40.0070.02917.80
7.0.30.0030.03517.79
7.0.20.0040.03117.85
7.0.10.0050.03717.94
7.0.00.0050.03217.83
5.6.400.0040.01114.73
5.6.390.0070.00714.42
5.6.380.0000.01414.89
5.6.370.0070.00414.47
5.6.360.0040.01114.60
5.6.350.0040.01114.89
5.6.340.0050.00814.71
5.6.330.0100.00314.55
5.6.320.0090.00614.85
5.6.310.0030.01314.83
5.6.300.0030.01014.66
5.6.290.0040.01114.50
5.6.280.0100.02817.91
5.6.270.0070.03517.91
5.6.260.0050.03317.72
5.6.250.0070.03217.86
5.6.240.0030.03817.82
5.6.230.0090.03017.59
5.6.220.0030.03717.79
5.6.210.0030.03617.70
5.6.200.0030.03517.73
5.6.190.0070.03317.79
5.6.180.0070.03317.84
5.6.170.0090.03317.89
5.6.160.0050.03517.55
5.6.150.0070.03217.87
5.6.140.0080.03417.83
5.6.130.0020.03617.78
5.6.120.0070.03417.72
5.6.110.0100.03317.90
5.6.100.0050.03317.57
5.6.90.0150.02517.72
5.6.80.0120.02517.53
5.6.70.0050.03217.37
5.6.60.0060.03217.62
5.6.50.0080.02817.34
5.6.40.0080.03117.26
5.6.30.0070.03317.49
5.6.20.0050.03317.50
5.6.10.0080.03017.48
5.6.00.0080.03017.54

preferences:
62.55 ms | 400 KiB | 5 Q