3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); class StackOverflowCopyPaste { /** @var int $v4MaskBits */ private $v4MaskBits; /** @var int $v6MaskBits */ private $v6MaskBits; /** * @param int $v4MaskBits * @param int $v6MaskBits */ public function __construct(int $v4MaskBits = 24, int $v6MaskBits = 48) { $this->v4MaskBits = $v4MaskBits; $this->v6MaskBits = $v6MaskBits; } /** * Return the given subnet for an IP and the configured mask bits * * Determine if the IP is an IPv4 or IPv6 address, then pass to the correct * method for handling that specific type. * * @param string $ip * @return string */ public function getSubnet(string $ip): string { if (\preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $ip)) { return $this->getIPv4Subnet( $ip, (int) ($this->v4MaskBits ?? 32) ); } return $this->getIPv6Subnet( $ip, (int) ($this->v6MaskBits ?? 128) ); } /** * Return the given subnet for an IPv4 address and mask bits * * @param string $ip * @param int $maskBits * @return string */ public function getIPv4Subnet(string $ip, int $maskBits = 32): string { $binary = \inet_pton($ip); for ($i = 32; $i > $maskBits; $i -= 8) { $j = \intdiv($i, 8) - 1; $k = (int) \min(8, $i - $maskBits); $mask = (0xff - ((1 << $k) - 1)); $int = \unpack('C', $binary[$j]); $binary[$j] = \pack('C', $int[1] & $mask); } return \inet_ntop($binary).'/'.$maskBits; } /** * Return the given subnet for an IPv6 address and mask bits * * @param string $ip * @param int $maskBits * @return string */ public function getIPv6Subnet(string $ip, int $maskBits = 48): string { $binary = \inet_pton($ip); for ($i = 128; $i > $maskBits; $i -= 8) { $j = \intdiv($i, 8) - 1; $k = (int) \min(8, $i - $maskBits); $mask = (0xff - ((1 << $k) - 1)); $int = \unpack('C', $binary[$j]); $binary[$j] = \pack('C', $int[1] & $mask); } return \inet_ntop($binary).'/'.$maskBits; } } $mask = new StackOverflowCopyPaste(24, 48); var_dump($mask->getSubnet('12.34.56.78')); var_dump($mask->getSubnet('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));

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.70.0150.00418.96
8.3.60.0220.00018.68
8.3.50.0090.00618.24
8.3.40.0120.00319.09
8.3.30.0110.00419.26
8.3.20.0050.00319.15
8.3.10.0050.00323.75
8.3.00.0070.00020.89
8.2.190.0060.00917.11
8.2.180.0090.00618.66
8.2.170.0140.00722.96
8.2.160.0070.00722.38
8.2.150.0080.00024.18
8.2.140.0080.00024.66
8.2.130.0180.00020.08
8.2.120.0080.00026.35
8.2.110.0030.00720.56
8.2.100.0060.00618.09
8.2.90.0040.00418.16
8.2.80.0080.00017.97
8.2.70.0030.00618.18
8.2.60.0060.00318.34
8.2.50.0050.00318.10
8.2.40.0040.00419.40
8.2.30.0070.00418.78
8.2.20.0030.00918.21
8.2.10.0040.00418.21
8.2.00.0080.00017.99
8.1.280.0120.00625.92
8.1.270.0060.00318.96
8.1.260.0040.00426.35
8.1.250.0050.00328.09
8.1.240.0060.00320.98
8.1.230.0040.00722.78
8.1.220.0050.00317.77
8.1.210.0080.00019.04
8.1.200.0090.00017.47
8.1.190.0000.00817.78
8.1.180.0050.00318.10
8.1.170.0050.00318.71
8.1.160.0040.00418.89
8.1.150.0040.00418.89
8.1.140.0040.00422.23
8.1.130.0030.00320.29
8.1.120.0060.00317.66
8.1.110.0030.00517.57
8.1.100.0000.00717.71
8.1.90.0040.00417.71
8.1.80.0040.00417.66
8.1.70.0000.00717.70
8.1.60.0080.00017.69
8.1.50.0080.00017.80
8.1.40.0040.00417.76
8.1.30.0030.00617.90
8.1.20.0000.00917.81
8.1.10.0060.00317.69
8.1.00.0050.00317.75
8.0.300.0060.00319.88
8.0.290.0040.00416.75
8.0.280.0000.00718.48
8.0.270.0050.00317.39
8.0.260.0030.00320.34
8.0.250.0030.00317.17
8.0.240.0000.00817.20
8.0.230.0040.00417.19
8.0.220.0040.00417.10
8.0.210.0050.00217.17
8.0.200.0030.00317.13
8.0.190.0040.00417.19
8.0.180.0050.00317.18
8.0.170.0030.00617.18
8.0.160.0040.00417.13
8.0.150.0000.00717.02
8.0.140.0040.00416.95
8.0.130.0000.00513.61
8.0.120.0000.00817.13
8.0.110.0040.00417.16
8.0.100.0000.00717.06
8.0.90.0040.00416.99
8.0.80.0040.01117.20
8.0.70.0030.00517.21
8.0.60.0040.00416.96
8.0.50.0050.00317.07
8.0.30.0090.00917.34
8.0.20.0070.01717.40
8.0.10.0070.00017.21
8.0.00.0100.01016.86
7.4.330.0050.00015.55
7.4.320.0000.00716.87
7.4.300.0070.00016.78
7.4.290.0000.00716.88
7.4.280.0060.00316.75
7.4.270.0040.00416.96
7.4.260.0030.00416.84
7.4.250.0030.00516.77
7.4.240.0000.00716.95
7.4.230.0070.00016.69
7.4.220.0040.00416.89
7.4.210.0100.00816.84
7.4.200.0050.00316.52
7.4.160.0100.00816.86
7.4.140.0090.00817.86
7.4.130.0090.00916.79
7.4.120.0080.01116.89
7.4.110.0150.00316.83
7.4.100.0100.00716.70
7.4.90.0160.00916.75
7.4.80.0180.00619.39
7.4.70.0120.01216.81
7.4.60.0150.00516.68
7.4.50.0100.00716.72
7.4.40.0070.01216.75
7.4.30.0110.01216.54
7.4.20.0100.01416.84
7.4.10.0140.00516.86
7.4.00.0080.00716.22
7.3.330.0050.00013.45
7.3.320.0000.00513.55
7.3.310.0070.00016.45
7.3.300.0040.00416.46
7.3.290.0070.00016.55
7.3.280.0090.00816.54
7.3.260.0100.01016.70
7.3.240.0120.00516.61
7.3.230.0110.00716.89
7.3.210.0080.00816.66
7.3.200.0120.00616.65
7.3.190.0080.01116.70
7.3.180.0110.00716.85
7.3.170.0140.00816.54
7.3.160.0110.01016.63
7.3.150.0120.00816.67
7.3.140.0080.01016.79
7.3.130.0100.00816.66
7.3.120.0120.00716.73
7.3.110.0120.00615.91
7.3.100.0080.00915.80
7.3.90.0080.00815.86
7.3.80.0090.00815.91
7.3.70.0100.00715.87
7.3.60.0120.00915.84
7.3.50.0110.00815.74
7.3.40.0090.00915.56
7.3.30.0100.00715.62
7.3.20.0140.00816.09
7.3.10.0130.00816.08
7.3.00.0250.00915.99
7.2.330.0080.01116.88
7.2.320.0120.00916.96
7.2.310.0080.01116.90
7.2.300.0160.00316.82
7.2.290.0090.01316.98
7.2.280.0080.01216.94
7.2.270.0070.01217.07
7.2.260.0080.01117.03
7.2.250.0110.00816.90
7.2.240.0080.01016.15
7.2.230.0080.01016.18
7.2.220.0090.00916.13
7.2.210.0090.01116.02
7.2.200.0120.00616.06
7.2.190.0070.01416.18
7.2.180.0110.00916.15
7.2.170.0100.00915.90
7.2.160.0090.00915.89
7.2.150.0130.01116.31
7.2.140.0160.01116.27
7.2.130.0140.00916.28
7.2.120.0300.00616.13
7.2.110.0430.00815.92
7.2.100.0410.00816.02
7.2.90.0340.00915.99
7.2.80.0370.00716.11
7.2.70.0330.00716.06
7.2.60.0420.00916.03
7.2.50.0390.00816.06
7.2.40.0360.01016.08
7.2.30.0350.00716.16
7.2.20.0390.01016.07
7.2.10.0360.00816.00
7.2.00.0310.00916.17
7.1.330.0080.01415.08
7.1.320.0140.00715.02
7.1.310.0110.01215.04
7.1.300.0160.00715.08
7.1.290.0150.00815.02
7.1.280.0190.00715.32
7.1.270.0140.00515.39
7.1.260.0140.00615.21
7.1.250.0180.00915.18
7.1.240.0460.00715.08
7.1.230.0470.00814.86
7.1.220.0440.00614.88
7.1.210.0370.00814.80
7.1.200.0560.00914.92
7.1.190.0390.00814.96
7.1.180.0460.00514.90
7.1.170.0380.01114.94
7.1.160.0450.00914.91
7.1.150.0390.00714.84
7.1.140.0410.00614.92
7.1.130.0370.00714.93
7.1.120.0430.01014.82
7.1.110.0310.01315.01
7.1.100.0430.01014.95
7.1.90.0360.00714.87
7.1.80.0420.01015.01
7.1.70.0430.00714.87
7.1.60.0370.01016.98
7.1.50.0370.00916.99
7.1.40.0320.01016.90
7.1.30.0360.01216.95
7.1.20.0460.01016.92
7.1.10.0340.00914.94
7.1.00.0330.00814.86
7.0.330.0210.00814.81
7.0.320.0150.01115.53
7.0.310.0160.01315.36
7.0.300.0260.00315.60
7.0.290.0200.01015.41
7.0.280.0250.00715.54
7.0.270.0180.01115.54
7.0.260.0370.00815.42
7.0.250.0240.00515.37
7.0.240.0200.01215.60
7.0.230.0150.01415.48
7.0.220.0210.00915.55
7.0.210.0150.01515.38
7.0.200.0250.00915.47
7.0.190.0230.00915.37
7.0.180.0170.01015.56
7.0.170.0250.01015.55
7.0.160.0210.00915.40
7.0.150.0160.00915.51
7.0.140.0150.01215.68
7.0.130.0160.00815.52
7.0.120.0170.01115.53
7.0.110.0190.01015.53
7.0.100.0250.00815.57
7.0.90.0190.00815.49
7.0.80.0180.01315.47
7.0.70.0230.00615.46
7.0.60.0190.01015.37
7.0.50.0130.01015.47
7.0.40.0320.00815.59
7.0.30.0170.00715.53
7.0.20.0160.00815.40
7.0.10.0160.00915.50
7.0.00.0250.00815.48
5.6.400.0120.00814.54
5.6.390.0120.00814.57
5.6.380.0120.00914.33
5.6.370.0150.01515.74
5.6.360.0160.01115.46
5.6.350.0170.00915.53
5.6.340.0190.00915.39
5.6.330.0140.01215.63
5.6.320.0220.00915.54
5.6.310.0190.00815.72
5.6.300.0120.01515.54
5.6.290.0110.01515.65
5.6.280.0190.00915.66
5.6.270.0260.00415.61
5.6.260.0150.01115.63
5.6.250.0220.01215.69
5.6.240.0160.01015.58
5.6.230.0130.01315.58
5.6.220.0150.01015.60
5.6.210.0230.00615.55
5.6.200.0120.01515.74
5.6.190.0160.01215.54
5.6.180.0140.01315.58
5.6.170.0170.01015.72
5.6.160.0190.01115.54
5.6.150.0130.01315.59
5.6.140.0150.01215.63
5.6.130.0110.01815.49
5.6.120.0120.01415.70
5.6.110.0210.00815.74
5.6.100.0160.01115.63
5.6.90.0160.01115.71
5.6.80.0110.01315.60
5.6.70.0130.01115.42
5.6.60.0090.01315.54
5.6.50.0130.01115.45
5.6.40.0140.01215.30
5.6.30.0160.00915.53
5.6.20.0190.00815.47
5.6.10.0070.01815.50
5.6.00.0140.01215.45

preferences:
49.14 ms | 404 KiB | 6 Q