3v4l.org

run code in 300+ PHP versions simultaneously
<?php class StreamResponse { /** * @var string|null */ private $path; /** * @var resource|null */ private $stream; /** * @var int */ private $buffer = 102400; /** * @var int */ private $start = -1; /** * @var int */ private $end = -1; /** * @var int */ private $size = 0; /** * @var array<string, array<int, string>> */ private $headers; /** * @var int */ private $status; public function __construct(string $filePath, array $headers = [], int $status = 200) { $this->path = $filePath; $this->headers = $headers; $this->status = $status; } /** * Start streaming video content. */ public function send(): void { $this->open(); $this->setHeader(); $this->stream(); fclose($this->stream); } /** * Open stream. * * * @throws RuntimeException */ private function open(): void { if (!($this->stream = fopen($this->path, 'rb'))) { throw new RuntimeException('Could not open stream for reading'); } } /** * Set proper header to serve the video content. */ private function setHeader(): void { ob_get_clean(); http_response_code($this->status); foreach ($this->headers as $header => $values) { $values = (array) $values; header($header . ': ' . implode(',', $values)); } header('Content-Type: ' . mime_content_type($this->path), false); header('Cache-Control: max-age=2592000, public', false); header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 2592000) . ' GMT', false); header('Last-Modified: ' . gmdate('D, d M Y H:i:s', @filemtime($this->path)) . ' GMT', false); $this->start = 0; $this->size = filesize($this->path); $this->end = $this->size - 1; header('Accept-Ranges: 0-' . $this->end); if (isset($_SERVER['HTTP_RANGE'])) { $c_end = $this->end; [, $range] = explode('=', $_SERVER['HTTP_RANGE'], 2); if (false !== strpos($range, ',')) { header('HTTP/1.1 416 Requested Range Not Satisfiable'); header("Content-Range: bytes $this->start-$this->end/$this->size"); return; } if ('-' === $range) { $c_start = $this->size - substr($range, 1); } else { $range = explode('-', $range); $c_start = $range[0]; $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_end; } $c_end = ($c_end > $this->end) ? $this->end : $c_end; if ($c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size) { header('HTTP/1.1 416 Requested Range Not Satisfiable'); header('Content-Range: bytes ' . $this->start - $this->end . '/' . $this->size); return; } $this->start = $c_start; $this->end = $c_end; $length = $this->end - $this->start + 1; fseek($this->stream, $this->start); header('HTTP/1.1 206 Partial Content'); header('Content-Length: ' . $length); header('Content-Range: bytes ' . $this->start - $this->end . '/' . $this->size); } else { header('Content-Length: ' . $this->size); } } /** * perform the streaming of calculated range. */ private function stream(): void { $i = $this->start; set_time_limit(0); while (!feof($this->stream) && $i <= $this->end) { $bytesToRead = $this->buffer; if (($i + $bytesToRead) > $this->end) { $bytesToRead = $this->end - $i + 1; } $data = fread($this->stream, $bytesToRead); echo $data; flush(); $i += $bytesToRead; } } }

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.0.120.0050.00313.19
8.0.110.0060.00313.29
8.0.100.0040.00713.28
8.0.90.0030.00513.23
8.0.80.0040.00413.34
8.0.70.0040.00513.29
8.0.60.0040.00413.31
8.0.50.0060.00313.33
8.0.30.0080.00613.27
8.0.20.0150.00013.38
8.0.10.0050.01013.33
8.0.00.0000.00813.39
7.4.250.0100.00613.24
7.4.240.0000.01513.29
7.4.230.0130.00313.16
7.4.220.0090.00513.24
7.4.210.0000.01413.26
7.4.200.0080.00413.29
7.4.190.0100.00413.34
7.4.180.0080.00513.32
7.4.160.0070.00713.32
7.4.150.0110.00413.29
7.4.140.0120.00013.20
7.4.130.0090.00313.21
7.4.120.0090.00513.21
7.4.110.0090.00513.16
7.4.100.0150.00013.30
7.4.90.0080.00413.13
7.4.80.0140.00013.16
7.4.70.0120.00013.07
7.4.60.0130.00013.02
7.4.50.0090.00512.99
7.4.40.0090.00313.14
7.4.30.0100.00313.20
7.4.20.0130.00013.19
7.4.10.0120.00313.16
7.4.00.0090.00813.88
7.3.310.0090.00513.10
7.3.300.0110.00313.28
7.3.290.0100.00313.26
7.3.280.0100.00313.31
7.3.270.0100.00213.24
7.3.260.0040.00813.21
7.3.250.0100.00513.28
7.3.240.0100.00313.00
7.3.230.0090.00613.04
7.3.220.0100.00513.11
7.3.210.0160.00313.14
7.3.200.0130.00012.98
7.3.190.0000.01113.15
7.3.180.0060.00613.19
7.3.170.0030.00513.07
7.3.160.0030.00313.22
7.3.150.0040.00313.21
7.3.140.0140.00012.99
7.3.130.0110.00412.99
7.3.120.0060.00913.94
7.3.110.0100.00714.10
7.3.100.0090.00713.92
7.3.90.0080.00213.87
7.3.80.0080.00514.07
7.3.70.0040.00714.01
7.3.60.0060.00514.01
7.3.50.0070.00413.93
7.3.40.0070.00314.00
7.3.30.0060.00613.94
7.3.20.0060.00315.66
7.3.10.0050.00515.66
7.3.00.0060.00115.56
7.2.340.0080.00413.01
7.2.330.0080.00513.18
7.2.320.0090.00413.05
7.2.310.0080.00513.14
7.2.300.0060.00613.10
7.2.290.0090.00613.06
7.2.280.0090.00413.26
7.2.270.0070.00713.20
7.2.260.0120.00013.28
7.2.250.0130.00313.93
7.2.240.0040.00914.05
7.2.230.0050.00814.14
7.2.220.0080.00513.94
7.2.210.0090.00614.05
7.2.200.0080.00614.08
7.2.190.0100.00214.01
7.2.180.0080.00714.25
7.2.170.0080.00314.15
7.2.160.0080.00714.19
7.2.150.0070.00715.81
7.2.140.0090.00415.94
7.2.130.0080.00615.89
7.2.120.0030.00815.89
7.2.110.0080.00415.97
7.2.100.0490.00715.44
7.2.90.0500.00515.23
7.2.80.0460.00715.52
7.2.70.0400.00315.51
7.2.60.0450.00415.42
7.2.50.0400.00215.55
7.2.40.0390.00715.63
7.2.30.0410.00815.59
7.2.20.0370.00715.61
7.2.10.0390.00715.67
7.2.00.0460.01015.45
7.1.330.0110.00314.79
7.1.320.0080.00614.77
7.1.310.0100.00514.80
7.1.300.0050.01014.67
7.1.290.0080.00314.78
7.1.280.0070.00714.79
7.1.270.0080.00714.95
7.1.260.0090.00614.93
7.1.250.0070.00514.82
7.1.240.0070.00314.97
7.1.230.0020.01114.96
7.1.220.0520.00614.25
7.1.210.0470.00814.39
7.1.200.0480.00414.53
7.1.190.0320.00414.42
7.1.180.0400.00714.45
7.1.170.0340.00614.55
7.1.160.0390.00414.27
7.1.150.0370.00514.37
7.1.140.0660.00414.50
7.1.130.0490.00714.50
7.1.120.0380.00614.51
7.1.110.0510.00414.47
7.1.100.0480.00914.49
7.1.90.0380.00414.44
7.1.80.0420.00514.57
7.1.70.0400.00314.35
7.1.60.0500.00820.50
7.1.50.0520.00820.39
7.1.40.0500.00820.46
7.1.30.0640.00820.37
7.1.20.0650.00720.57
7.1.10.0520.00714.46
7.1.00.0380.00414.38
7.0.330.0110.00214.65
7.0.320.0090.00314.58
7.0.310.0070.00614.58
7.0.300.0060.00714.69
7.0.290.0090.00414.58
7.0.280.0060.00614.59
7.0.270.0070.00714.61
7.0.260.0060.00614.34
7.0.250.0080.00514.58
7.0.240.0030.00914.63
7.0.230.0070.00614.55
7.0.220.0110.00014.55
7.0.210.0120.00214.64
7.0.200.0100.00314.64
7.0.190.0040.00814.58
7.0.180.0050.00714.46
7.0.170.0060.00514.59
7.0.160.0080.00614.70
7.0.150.0110.00214.63
7.0.140.0060.00614.57
7.0.130.0070.00614.63
7.0.120.0080.00514.78
7.0.110.0090.00314.64
7.0.100.0100.00414.55
7.0.90.0060.00714.47
7.0.80.0070.00814.54
7.0.70.0060.00514.61
7.0.60.0070.00714.43
7.0.50.0060.00614.38
7.0.40.0060.00613.45
7.0.30.0050.00813.49
7.0.20.0050.00713.56
7.0.10.0070.00613.48
7.0.00.0100.00613.69
5.6.400.0040.00713.27
5.6.390.0020.01113.56
5.6.380.0090.00413.31
5.6.370.0070.00313.49
5.6.360.0110.00013.21
5.6.350.0060.00713.08
5.6.340.0080.00513.34
5.6.330.0070.00613.35
5.6.320.0040.01013.31
5.6.310.0090.00613.34
5.6.300.0100.00413.13
5.6.290.0080.00313.31
5.6.280.0060.00413.42
5.6.270.0070.00513.18
5.6.260.0070.00513.34
5.6.250.0090.00313.03
5.6.240.0070.00413.38
5.6.230.0050.00913.25
5.6.220.0090.00513.44
5.6.210.0100.00513.24
5.6.200.0110.00313.41
5.6.190.0090.00313.12
5.6.180.0050.00813.33
5.6.170.0030.01213.49
5.6.160.0080.00613.32
5.6.150.0040.01013.22
5.6.140.0060.00713.42
5.6.130.0100.00513.26
5.6.120.0070.00613.46
5.6.110.0060.00613.26
5.6.100.0080.00213.24
5.6.90.0070.00513.36
5.6.80.0070.00513.18
5.6.70.0060.00613.14
5.6.60.0070.00613.28
5.6.50.0110.00213.28
5.6.40.0050.01013.26
5.6.30.0060.00913.19
5.6.20.0070.00613.09
5.6.10.0090.00213.06
5.6.00.0100.00413.28

preferences:
22.42 ms | 401 KiB | 5 Q