3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Demo; class OutOfMemory extends \Exception {} class InvalidFree extends \Exception {} // This is the system heap, this system only has 128 bytes of memory available to applications const HEAP_SIZE = 128; $heap = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"; // The operating system allocator keeps track of the memory block that have been allocated to each application $blocks = []; function malloc($size) { global $blocks; $newPtr = 0; foreach ($blocks as $ptr => $size) { // If the gap between $newPtr and the start of this block is big enough to fit the request size, // allocate the block in between if ($newPtr + $size <= $ptr) { $blocks[$newPtr] = $size; return $newPtr; } $newPtr = $ptr + $size; } // Check if there's enough space at the end // When allocating at the end, verify that the requested size won't overflow the heap if ($newPtr + $size <= HEAP_SIZE) { $blocks[$newPtr] = $size; return $newPtr; } // There was not a large enough contiguous block on the heap to allocate the requested size throw new OutOfMemory; } function free($ptr) { global $blocks; // Can only free a complete block that has been allocated (can't free a partial one) if (!isset($blocks[$ptr])) { throw new InvalidFree; } unset($blocks[$ptr]); } // strcpy() is an example of a function that works with C strings and relies on the presence of a null terminator function strcpy($dstPtr, $srcPtr) { global $heap; for ($i = 0; $heap[$srcPtr + $i] !== "\0"; $i++) { $heap[$dstPtr + 0] = $heap[$srcPtr + $i]; } } // Output an arbitrary block from the heap // Notice that this requires a length argument so it knows when to stop function output($ptr, $len) { global $heap; for ($i = 0; $ptr + $i < HEAP_SIZE && $i < $len; $i++) { echo $heap[$ptr + $i]; } // This is just here to make the output readable echo "\n\n"; } // Output a string // No length argument here, so it must be a valid string function output_string($ptr) { global $heap; for ($i = 0; $ptr + $i < HEAP_SIZE && $heap[$ptr + $i] !== "\0"; $i++) { echo $heap[$ptr + $i]; } // This is just here to make the output readable echo "\n\n"; } $myString = malloc(14); $heap[$myString + 0] = 'h'; $heap[$myString + 1] = 'e'; $heap[$myString + 2] = 'l'; $heap[$myString + 3] = 'l'; $heap[$myString + 4] = 'o'; $heap[$myString + 5] = ','; $heap[$myString + 6] = ' '; $heap[$myString + 7] = 'w'; $heap[$myString + 8] = 'o'; $heap[$myString + 9] = 'r'; $heap[$myString + 10] = 'l'; $heap[$myString + 11] = 'd'; $heap[$myString + 12] = '!'; $heap[$myString + 13] = "\0"; // fine, this is a valid string, it has a null terminator output_string($myString); $arbitraryBlock = malloc(1); $heap[$arbitraryBlock] = '#'; // fine, we gave it a sensible length output($arbitraryBlock, 1); // but if we do this, we get an overflow and it keeps going until it encounters the next null byte or the end of the heap output_string($arbitraryBlock); // things can get weirder when stuff like this happens free($myString); // release the memory back to the OS $newString = malloc(9); // this will end up reallocating the same block $heap[$newString + 0] = 'H'; $heap[$newString + 1] = 'i'; $heap[$newString + 2] = ' '; $heap[$newString + 3] = 't'; $heap[$newString + 4] = 'h'; $heap[$newString + 5] = 'e'; $heap[$newString + 6] = 'r'; $heap[$newString + 7] = 'e'; // oops! no null terminator output_string($newString);

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.4.120.0120.00523.89
8.4.110.0100.01022.43
8.4.100.0080.00620.39
8.4.90.0060.00618.95
8.4.80.0070.00520.41
8.4.70.0090.00817.77
8.4.60.0130.00818.74
8.4.50.0120.01020.56
8.4.40.0150.00619.30
8.4.30.0030.00619.63
8.4.20.0030.00617.88
8.4.10.0040.00417.62
8.3.250.0150.00419.17
8.3.240.0050.00317.38
8.3.230.0130.00616.50
8.3.220.0120.00719.05
8.3.210.0130.00918.34
8.3.200.0070.00416.64
8.3.190.0140.00517.43
8.3.180.0110.00716.59
8.3.170.0090.00918.81
8.3.160.0040.01517.25
8.3.150.0120.00617.55
8.3.140.0040.00716.49
8.3.130.0120.00618.40
8.3.120.0030.00620.70
8.3.110.0060.00920.94
8.3.100.0090.00316.57
8.3.90.0040.00416.75
8.3.80.0070.00317.97
8.3.70.0150.00418.31
8.3.60.0120.00316.74
8.3.50.0080.01121.99
8.3.40.0130.00318.86
8.3.30.0040.01118.83
8.3.20.0080.00020.29
8.3.10.0030.00721.94
8.3.00.0050.00321.90
8.2.290.0060.00820.56
8.2.280.0110.00718.51
8.2.270.0110.00717.16
8.2.260.0070.01318.24
8.2.250.0060.00316.61
8.2.240.0090.00017.41
8.2.230.0030.00622.58
8.2.220.0060.00324.06
8.2.210.0060.00326.77
8.2.200.0090.00016.75
8.2.190.0100.01016.58
8.2.180.0150.00718.34
8.2.170.0160.00022.96
8.2.160.0080.00619.29
8.2.150.0040.00424.18
8.2.140.0030.00624.66
8.2.130.0040.00422.08
8.2.120.0060.00326.35
8.2.110.0090.00020.52
8.2.100.0060.00617.78
8.2.90.0000.00819.29
8.2.80.0000.00817.97
8.2.70.0030.00617.63
8.2.60.0050.00517.93
8.2.50.0030.00518.10
8.2.40.0080.00022.32
8.2.30.0040.00421.16
8.2.20.0040.00418.21
8.2.10.0000.00818.35
8.2.00.0000.00718.36
8.1.330.0130.00622.08
8.1.320.0100.01216.04
8.1.310.0050.00318.62
8.1.300.0030.00617.91
8.1.290.0060.00330.84
8.1.280.0180.00325.92
8.1.270.0090.00020.67
8.1.260.0000.00826.35
8.1.250.0030.00528.09
8.1.240.0000.00923.98
8.1.230.0090.00317.83
8.1.220.0060.00317.80
8.1.210.0040.00818.77
8.1.200.0000.01017.35
8.1.190.0030.00517.35
8.1.180.0060.00318.10
8.1.170.0040.00421.99
8.1.160.0000.00718.93
8.1.150.0000.00720.15
8.1.140.0070.00019.59
8.1.130.0030.00518.98
8.1.120.0040.00417.50
8.1.110.0040.00417.39
8.1.100.0060.00317.50
8.1.90.0000.00717.44
8.1.80.0040.00417.55
8.1.70.0000.00717.47
8.1.60.0030.00617.50
8.1.50.0000.00817.61
8.1.40.0040.00417.57
8.1.30.0080.00017.71
8.1.20.0040.00417.63
8.1.10.0040.00417.67
8.1.00.0070.00317.39
8.0.300.0000.00818.77
8.0.290.0070.00016.75
8.0.280.0000.00718.50
8.0.270.0030.00317.35
8.0.260.0070.00017.30
8.0.250.0030.00316.98
8.0.240.0000.00716.96
8.0.230.0070.00017.06
8.0.220.0070.00016.97
8.0.210.0000.00716.95
8.0.200.0000.00716.97
8.0.190.0000.00816.99
8.0.180.0000.00817.00
8.0.170.0000.00717.06
8.0.160.0040.00416.94
8.0.150.0040.00416.84
8.0.140.0040.00416.85
8.0.130.0030.00313.38
8.0.120.0030.00516.93
8.0.110.0050.00316.89
8.0.100.0040.00416.97
8.0.90.0040.00417.01
8.0.80.0080.00816.93
8.0.70.0070.00016.84
8.0.60.0000.00816.76
8.0.50.0000.00816.79
8.0.30.0090.00817.27
8.0.20.0110.01217.40
8.0.10.0040.00416.84
8.0.00.0060.01316.75
7.4.330.0000.00615.55
7.4.320.0030.00316.57
7.4.300.0000.00616.41
7.4.290.0000.00716.51
7.4.280.0000.00716.61
7.4.270.0030.00316.67
7.4.260.0070.00016.61
7.4.250.0000.00816.61
7.4.240.0030.00416.63
7.4.230.0040.00416.66
7.4.220.0070.01316.56
7.4.210.0110.00316.61
7.4.200.0000.00716.46
7.4.160.0060.01316.60
7.4.150.0090.01217.40
7.4.140.0140.00717.86
7.4.130.0090.01416.60
7.4.120.0130.01116.62
7.4.110.0150.00416.77
7.4.100.0130.00516.52
7.4.90.0160.00316.38
7.4.80.0120.01519.39
7.4.70.0140.00316.59
7.4.60.0100.00616.63
7.4.50.0110.00416.60
7.4.40.0040.01216.54
7.4.30.0070.01016.45
7.4.00.0100.00915.02
7.3.330.0070.00013.26
7.3.320.0000.00613.45
7.3.310.0030.00516.47
7.3.300.0020.00516.33
7.3.290.0080.01016.43
7.3.280.0100.01016.43
7.3.270.0160.00617.40
7.3.260.0200.01116.42
7.3.250.0070.01016.55
7.3.240.0060.01316.58
7.3.230.0100.00816.70
7.3.210.0070.01316.52
7.3.200.0080.00916.49
7.3.190.0070.01016.44
7.3.180.0160.00316.63
7.3.170.0100.00716.73
7.3.160.0170.00016.62
7.3.120.0060.01114.93
7.3.110.0110.00815.05
7.3.100.0020.01314.82
7.3.90.0010.01314.74
7.3.80.0020.00714.85
7.3.70.0090.00914.99
7.3.60.0040.01114.89
7.3.50.0050.00814.96
7.3.40.0070.00914.73
7.3.30.0080.00715.00
7.3.20.0050.00716.74
7.3.10.0090.00616.67
7.3.00.0020.01416.58
7.2.330.0110.00816.84
7.2.320.0090.00916.80
7.2.310.0180.00016.86
7.2.300.0210.00716.74
7.2.290.0070.01016.70
7.2.250.0040.01515.28
7.2.240.0050.01515.26
7.2.230.0070.00715.27
7.2.220.0030.00815.15
7.2.210.0060.00915.12
7.2.200.0100.00515.06
7.2.190.0050.01315.22
7.2.180.0050.01214.94
7.2.170.0080.00815.24
7.2.160.0100.00615.26
7.2.150.0120.00316.97
7.2.140.0110.00317.02
7.2.130.0070.01117.08
7.2.120.0150.00316.60
7.2.110.0070.00317.02
7.2.100.0000.01516.79
7.2.90.0070.01017.10
7.2.80.0110.00016.96
7.2.70.0070.00717.03
7.2.60.0040.00817.16
7.2.50.0060.00616.89
7.2.40.0030.00916.79
7.2.30.0080.00616.89
7.2.20.0060.00917.14
7.2.10.0100.00617.01
7.2.00.0080.00816.95
7.1.330.0070.00815.66
7.1.320.0050.00515.80
7.1.310.0050.01015.81
7.1.300.0040.01215.85
7.1.290.0110.00815.74
7.1.280.0030.00815.70
7.1.270.0020.01115.82
7.1.260.0070.00415.72
7.1.250.0110.00715.68
7.1.200.0130.00815.71
7.1.70.0030.01017.09
7.1.60.0070.00717.04
7.1.30.0150.01534.40
7.1.20.0200.01034.39
7.1.10.0000.01316.62
7.1.00.0070.01016.71
7.0.200.0070.00716.75
7.0.170.0030.01016.35
7.0.160.0030.01015.97
7.0.150.0030.01016.13
7.0.140.0000.01316.41
7.0.130.0070.00716.36
7.0.120.0030.01016.55
7.0.110.0000.02016.17
7.0.100.0000.01316.16
7.0.90.0030.01016.15
7.0.80.0030.01016.30
7.0.70.0070.00716.12
7.0.60.0070.00716.02
7.0.50.0000.01716.15
7.0.40.0070.01316.39
7.0.30.0070.00716.39
7.0.20.0030.01016.46
7.0.10.0000.01316.24
7.0.00.0070.01016.24

preferences:
30.38 ms | 403 KiB | 5 Q