3v4l.org

run code in 500+ PHP versions simultaneously
<?php // Crate json string contain 50000 objecy $locations = ""; for ($i=0; $i<50000; $i++){ $locations .= "{\"location\":\"$i\",\"distance\":\"25.75206\"},"; } $json = "[".substr($locations, 0, -1)."]"; $location = '100'; // Using json_decode and loop $time = microtime(true); $locations = json_decode($json, true); foreach ($locations as $key => $value) { if ($value['location'] == $location) { @$distance = $value['distance']; } } echo "json_decode and loop:\n".(microtime(true) - $time)."\n"; // Using regex $time2 = microtime(true); if (preg_match("/\{\s*\"location\"\s*:\s*\"{$location}\".*?}/", $json, $match)){ $locations = json_decode("[{$match[0]}]", true)[0]; $distance = $locations['distance']; } echo "regex: \n".(microtime(true) - $time2);
Output for 8.2.28
json_decode and loop: 0.034378051757812 regex: 0.0021860599517822
Output for 8.1.3
Fatal error: Out of memory (allocated 31551488) (tried to allocate 20480 bytes) in /in/VCa0c on line 12 mmap() failed: [12] Cannot allocate memory mmap() failed: [12] Cannot allocate memory
Process exited with code 255.
Output for 7.3.0
json_decode and loop: 0.23745203018188 regex: 0.0051701068878174
Output for 7.2.13
json_decode and loop: 0.36746001243591 regex: 0.0049381256103516
Output for 7.2.12
json_decode and loop: 0.29263687133789 regex: 0.017539024353027
Output for 7.2.11
json_decode and loop: 0.62995886802673 regex: 0.010541200637817
Output for 7.2.10
json_decode and loop: 0.39107894897461 regex: 0.010406970977783
Output for 7.2.9
json_decode and loop: 0.48810911178589 regex: 0.014389991760254
Output for 7.2.8
json_decode and loop: 0.73709511756897 regex: 0.0098111629486084
Output for 7.2.7
json_decode and loop: 0.47811007499695 regex: 0.01335597038269
Output for 7.2.6
json_decode and loop: 0.397136926651 regex: 0.011240005493164
Output for 7.2.5
json_decode and loop: 0.40517616271973 regex: 0.010747194290161
Output for 7.2.4
json_decode and loop: 0.46790790557861 regex: 0.0082411766052246
Output for 7.2.3
json_decode and loop: 0.46867299079895 regex: 0.012545108795166
Output for 7.2.2
json_decode and loop: 0.67841601371765 regex: 0.010003089904785
Output for 7.2.1
json_decode and loop: 0.63826894760132 regex: 0.01429009437561
Output for 7.2.0
json_decode and loop: 0.89702105522156 regex: 0.0056540966033936
Output for 7.1.25
json_decode and loop: 0.28426003456116 regex: 0.0063188076019287
Output for 7.0.33
json_decode and loop: 0.45586204528809 regex: 0.0059690475463867
Output for 5.6.38
json_decode and loop: 0.13096189498901 regex: 0.018885850906372

preferences:
57.71 ms | 560 KiB | 4 Q