3v4l.org

run code in 300+ PHP versions simultaneously
<?php /*TODO... parse the sql query make a query object or something to put almost everything in an object and make this code simpler return nested name-value pairs in an associative array and convert it to json before sending aa s JSON content type send all changes made from Deletes, Updates and Append (mutations) changes for passive data binding */ $sql = "SELECT * FROM 'main' WHERE 'destination' = 'Helsinki' AND 'price' > 120"; //example of sql query //the following assumes the above SQL query has been parsed into fields and conditions //main header file. Stored as JSON. $main_db_header_contents = '[{"rowlength":60},{"id":{"length":3,"datatype":"integer"}},{"name_sname":{"length":9,"datatype":"String"}},{"identity":{"length":9,"datatype":"integer"}},{"address":{"length":8,"datatype":"String"}},{"region":{"length":7,"datatype":"String"}},{"hours":{"length":5,"datatype":"Time"}},{"destination":{"length":8,"datatype":"String"}},{"price":{"length":3,"datatype":"integer"}}]'; //json string. fairly immutable so it can be cached on server $main_db_header = json_decode($main_db_header_contents,true); //main database file. Columns are fixed length and padded. Stored as strings only. ID field must be sorted alphabetically. //When fields in this file are updated/added to, their respective index file must be too $main_db_file_contents = "001|Al-Farabi|123456789|Baghdad |Iraq |21:02|Ottawa |120;002|Smith |123456788|Ottawa |Canada |22:03|Helsinki|131;003|Locke |123456787|Helsinki|Finland|23:01|Istanbul|122;004|Jones |123456786|Istanbul|Turkey |21:02|Helsinki|123;005|Steinman |123455785|Havana |Cuba |22:03|Havana |104;006|Booker |123456784|Moscow |Russia |23:01|Helsinki|115;"; //fixed-length field values //Destination index file with record id and destination value. Sorted alphabetically. //E.g., The record with the id of 004 has the destination value of 'Moscow '. $destination_index_file_contents = "005|Havana ;002|Helsinki;004|Helsinki;006|Helsinki;003|Istanbul;001|Ottawa "; //Destination index file. Sorted alphabetically despite being numberic. //E.g., The record with the id of 005 has the price value of 104. $price_index_file_contents = "005|104;006|115;001|120;003|122;004|123;002|131"; function apply_condition($index_file_contents, $ptype, $value) { //applies basic logical conditions from the the query //Uses the given index file and checks its value based on given condition and given value $found_matches = []; $index_file_contents_split = explode(";", $index_file_contents); foreach($index_file_contents_split as $key => $props){ $pairs = explode("|",$props); $id = $pairs[0]; $val = trim($pairs[1]); //removes trailing spaces for matching non-fiexed-length input value if ($ptype === "=") { //checks to see if the given value is equal to the value in the index file if ($val === $value) { array_push($found_matches,$id); //add to new array of matches } } elseif ($ptype === ">") { //checks to see if the given value is greater than the value in the index file if ($val > $value) { array_push($found_matches,$id);//add to new array of matches } } } return $found_matches; } $parsed_destinations = apply_condition($destination_index_file_contents, "=", "Helsinki"); $parsed_prices = apply_condition($price_index_file_contents, ">", "120"); $parsed_ids = array_intersect($parsed_destinations, $parsed_prices); sort($parsed_ids); //alphabetically sorts array by ascending (lowest to highest) //now go through $main_db_file_contents and get selected fields in records matching the ids in $parsed_ids. $id_length = $main_db_header[1]['id']['length']; //id, length, datatype $destination_offset = get_field_offset("destination", $main_db_header); $price_offset = get_field_offset("price", $main_db_header); $destination_length = $main_db_header[7]['destination']['length']; //8 $price_length = $main_db_header[8]['price']['length']; //3 $row_length = $main_db_header[0]['rowlength']; //147 $main_file_length = strlen($main_db_file_contents); //can be done on a text file using the OS $total_main_file_rows = ($main_file_length/$row_length); $results =[]; $starting_row = 0; for ($i = 0; $i < count($parsed_ids); $i++){ //Starting row is advanced (increased) with each loop. It advances half as far as the previous advancement $starting_row = get_starting_row($main_db_file_contents, $parsed_ids[$i], $id_length, $row_length, $total_main_file_rows, $starting_row); $destination = get_field_from_row($main_db_file_contents, $starting_row, $row_length, $destination_offset, $destination_length); $price = get_field_from_row($main_db_file_contents, $starting_row, $row_length, $price_offset, $price_length); $result = [$parsed_ids[$i], $destination, $price]; array_push($results, $result); } function get_field_from_row($db, $current_row, $row_length, $field_offset, $field_length) { //gets the row index and uses the field's offset (start) and length (end) to get its value in the database text $value = substr($db, ($current_row * $row_length) + $field_offset, $field_length); return $value; } function get_starting_row($db, $first_id, $id_length, $row_length, $total_main_file_rows, $offset){ //Finds the record that the given ID is located in and returns its row index $start_row = (int)floor($total_main_file_rows / 2) + $offset; //halfway point of file $start_row_id = substr($db, ($start_row) * $row_length , $id_length); //gets current row id while ($start_row > -1 && $start_row <= $total_main_file_rows) { if ( $first_id < $start_row_id){ //go back a bit less $start_row = (int)floor($start_row / 2); $start_row_id = substr($db, ($start_row) * $row_length , $id_length); } elseif($start_row_id < $first_id) { //go forward a bit less $start_row = $start_row + (int)round($start_row / 2); $start_row_id = substr($db, ($start_row) * $row_length , $id_length); } else { //found the row id that matches the given id. Return its index. return $start_row; } } } function get_field_offset($field, $header) { //goes through the JSON file to get length of each field preceding the given field //used to get the start of the field in the row (a row offset is needed too) $total_offset = 0; foreach ($header as $key => $value){ foreach ($value as $k => $val) { if ($k != "rowlength"){ if ($field === (string)$k){ return $total_offset; } $total_offset += $val['length'] +1; //+1 is needed for each single-character delimeter (|) } } } } var_dump($results); //works!!

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.0130.00717.00
8.3.50.0090.00916.65
8.3.40.0110.00421.85
8.3.30.0090.00918.70
8.3.20.0030.01024.18
8.3.10.0060.00324.66
8.3.00.0080.00026.16
8.2.180.0130.00925.92
8.2.170.0110.00419.17
8.2.160.0070.01022.96
8.2.150.0000.00725.66
8.2.140.0040.00424.66
8.2.130.0110.00426.16
8.2.120.0080.00026.35
8.2.110.0100.00020.86
8.2.100.0060.00617.97
8.2.90.0030.00518.00
8.2.80.0060.00319.07
8.2.70.0000.00817.63
8.2.60.0040.00417.88
8.2.50.0000.00818.05
8.2.40.0000.00918.30
8.2.30.0000.00818.27
8.2.20.0030.00518.20
8.2.10.0040.00419.32
8.2.00.0000.00819.32
8.1.280.0150.00625.92
8.1.270.0030.00623.99
8.1.260.0080.00028.09
8.1.250.0050.00328.09
8.1.240.0030.00719.26
8.1.230.0030.00922.73
8.1.220.0060.00318.81
8.1.210.0080.00018.77
8.1.200.0040.00417.64
8.1.190.0030.00517.35
8.1.180.0100.00018.10
8.1.170.0000.00818.75
8.1.160.0000.00818.96
8.1.150.0040.00419.02
8.1.140.0040.00418.91
8.1.130.0030.00320.34
8.1.120.0040.00417.60
8.1.110.0060.00317.62
8.1.100.0030.00517.50
8.1.90.0050.00217.62
8.1.80.0070.00017.59
8.1.70.0040.00417.48
8.1.60.0030.00517.62
8.1.50.0000.00817.64
8.1.40.0040.00417.67
8.1.30.0000.00817.69
8.1.20.0050.00317.80
8.1.10.0080.00017.72
8.1.00.0080.00017.70
8.0.300.0000.00818.77
8.0.290.0050.00316.63
8.0.280.0050.00218.58
8.0.270.0000.00718.22
8.0.260.0000.00720.17
8.0.250.0030.00317.09
8.0.240.0000.00817.09
8.0.230.0000.00717.08
8.0.220.0000.00817.01
8.0.210.0040.00417.05
8.0.200.0070.00016.96
8.0.190.0050.00317.09
8.0.180.0040.00417.08
8.0.170.0060.00316.95
8.0.160.0030.00517.13
8.0.150.0000.00816.98
8.0.140.0050.00217.06
8.0.130.0030.00313.46
8.0.120.0040.00417.07
8.0.110.0040.00416.98
8.0.100.0070.00016.95
8.0.90.0040.00416.95
8.0.80.0050.00917.01
8.0.70.0060.00316.98
8.0.60.0000.00717.11
8.0.50.0040.00417.10
8.0.30.0090.00917.14
8.0.20.0090.01017.09
8.0.10.0000.00717.27
8.0.00.0160.00317.01
7.4.330.0000.00515.55
7.4.320.0000.00716.57
7.4.300.0030.00316.51
7.4.290.0070.00016.66
7.4.280.0000.00816.64
7.4.270.0000.00716.66
7.4.260.0000.00613.44
7.4.250.0050.00316.73
7.4.240.0050.00216.70
7.4.230.0000.00716.50
7.4.220.0020.00516.45
7.4.210.0060.01016.79
7.4.200.0040.00416.72
7.4.130.0110.01116.59
7.4.120.0060.01316.71
7.4.110.0130.00916.52
7.4.100.0120.01216.78
7.4.90.0140.00816.58
7.4.80.0130.01116.66
7.4.70.0150.00716.39
7.4.60.0130.00916.52
7.4.50.0100.01216.65
7.4.40.0110.00816.63
7.4.30.0130.00916.52
7.4.20.0140.00916.53
7.4.10.0100.01116.41
7.4.00.0130.00616.58
7.3.330.0070.00016.47
7.3.320.0030.00313.37
7.3.310.0040.00416.59
7.3.300.0040.00416.51
7.3.290.0080.00816.47
7.3.260.0110.00716.48
7.3.230.0160.00816.52
7.3.220.0120.01516.57
7.3.210.0150.01016.59
7.3.200.0140.01216.47
7.3.190.0110.01116.50
7.3.180.0130.01016.35
7.3.170.0110.00916.44
7.3.160.0130.01116.53
7.3.150.0120.00716.41
7.3.140.0120.01116.46
7.3.130.0110.00916.44
7.3.120.0130.00816.40
7.3.110.0110.01016.40
7.3.100.0100.00916.45
7.3.90.0100.01116.49
7.3.80.0130.00616.40
7.3.70.0130.00716.38
7.3.60.0090.01216.47
7.3.50.0130.00816.47
7.3.40.0140.01016.50
7.3.30.0100.01116.66
7.3.20.0150.00716.60
7.3.10.0130.01016.54
7.3.00.0130.00616.43
7.2.340.0160.00916.75
7.2.330.0190.00916.73
7.2.320.0120.01216.84
7.2.310.0130.01016.78
7.2.300.0110.00816.81
7.2.290.0120.00816.63
7.2.280.0100.01016.77
7.2.270.0120.01116.74
7.2.260.0150.00816.61
7.2.250.0120.01016.65
7.2.240.0060.01516.73
7.2.230.0080.01216.60
7.2.220.0090.01116.71
7.2.210.0130.00816.54
7.2.200.0130.00816.64
7.2.190.0160.00816.81
7.2.180.0110.01216.76
7.2.170.0110.01016.79
7.2.160.0130.00816.71
7.2.150.0160.00916.90
7.2.140.0170.00616.83
7.2.130.0130.00916.79
7.2.120.0120.01116.81
7.2.110.0100.01316.79
7.2.100.0120.01116.61
7.2.90.0130.01216.75
7.2.80.0170.00816.63
7.2.70.0140.01116.63
7.2.60.0160.00916.79
7.2.50.0090.01016.74
7.2.40.0070.01516.81
7.2.30.0110.01016.79
7.2.20.0180.01316.72
7.2.10.0270.00816.79
7.2.00.0130.00816.74

preferences:
64.09 ms | 401 KiB | 5 Q