3v4l.org

run code in 300+ PHP versions simultaneously
<?php function starts_with_strpos($haystack, $needle) { return strpos($haystack, $needle) === 0; } function starts_with_substr($haystack, $needle) { return substr($haystack, 0, strlen($needle)) === $needle; } // setup the haystack and needle $haystack = "Hello PHP"; $needle = "Hello"; // initialize the results array $results = array('substr' => array(), 'strpos' => array(), 'strpos_without_function' => array()); // exhaust the gc's threshhold at 10K $it = 10000; // Run the substr benchmark for($i = 0; $i < $it; $i++) { $t = microtime(true); starts_with_substr($haystack, $needle); $results['substr'][] = microtime(true) - $t; } // discard the first and last results as opline biased array_pop($results['substr']); array_shift($results['substr']); // Run the strpos benchmark for($i = 0; $i < $it; $i++) { $t = microtime(true); starts_with_strpos($haystack, $needle); $results['strpos'][] = microtime(true) - $t; } // discard the first and last results as opline biased array_pop($results['strpos']); array_shift($results['strpos']); // Run the strpos without a wrapper function benchmark for($i = 0; $i < $it; $i++) { $t = microtime(true); strpos($haystack, $needle) === 0; $results['strpos_without_function'][] = microtime(true) - $t; } // discard the first and last results as opline biased array_pop($results['strpos_without_function']); array_shift($results['strpos_without_function']); // compare the results $a = array_sum($results['substr']) / $it; $b = array_sum($results['strpos']) / $it; $c = array_sum($results['strpos_without_function']) / $it; printf("substr: %.9fs/iteration @ %d iterations\nstrpos: %.9fs/iteration @ %d iterations\nstrpos (without wrapper): %.9fs/iteration @ %d iterations\n\n", $a, $it, $b, $it, $c, $it); // determine which is faster (substr or strpos) if ($a > $b) { // substr is slower printf("strpos is %d%% faster than substr\n", $b / $a * 100); } elseif ($b > $a) { // strpos is slower printf("substr is %d%% faster than strpos\n", $a / $b * 100); } else { // they're the same printf("strpos and substr are the same\n"); } // determine which is faster (strpos with a wrapper or without) if ($b > $c) { // strpos wrapper is slower printf("strpos without a wrapper function is %d%% faster than strpos with a wrapper function\n", $c / $b * 100); } elseif ($c > $b) { // strpos wrapper is faster printf("strpos with a wrapper function is %d%% faster than strpos without a wrapper fucntion\n", $b / $c * 100); } else { // they're the same printf("strpos and substr are the same\n"); }

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)
7.4.00.0030.02216.18
7.3.120.0030.02815.80
7.3.110.0000.03015.98
7.3.100.0040.01915.72
7.3.90.0130.01316.00
7.3.80.0090.01816.29
7.3.70.0100.01315.96
7.3.60.0070.01715.95
7.3.50.0040.02115.99
7.3.40.0060.01615.98
7.3.30.0030.02415.93
7.3.20.0120.01518.47
7.3.10.0090.01518.59
7.3.00.0070.01318.73
7.2.250.0060.02216.39
7.2.240.0200.01416.30
7.2.230.0030.02416.19
7.2.220.0090.01816.31
7.2.210.0030.01916.01
7.2.200.0070.02016.21
7.2.190.0070.02015.99
7.2.180.0030.02416.14
7.2.170.0000.03016.25
7.2.00.0030.01721.63
7.1.330.0070.02617.77
7.1.320.0120.01817.74
7.1.310.0070.02317.79
7.1.300.0030.02417.99
7.1.290.0040.02517.81
7.1.280.0070.02518.03
7.1.270.0140.01717.83
7.1.260.0070.02717.85
7.1.70.0000.01418.82
7.1.60.0130.02319.70
7.1.50.0130.02618.56
7.1.00.0130.08324.19
7.0.200.0830.01016.81
7.0.140.0000.09324.07
7.0.60.0000.05021.20
7.0.50.0030.04718.98
7.0.40.0030.08720.27
7.0.30.0270.07720.23
7.0.20.0330.08720.08
7.0.10.0070.09720.08
7.0.00.0070.09320.16
5.6.280.0070.10324.92
5.6.210.0030.09324.80
5.6.200.0070.07322.37
5.6.190.0030.06724.59
5.6.180.0230.05724.45
5.6.170.0200.06324.59
5.6.160.0070.05724.49
5.6.150.0100.05022.27
5.6.140.0100.10722.25
5.6.130.0070.11722.27
5.6.120.0100.10325.16
5.6.110.0000.10725.27
5.6.100.0000.06325.13
5.6.90.0130.10725.13
5.6.80.0030.05724.68
5.5.350.3370.06324.52
5.5.340.0170.09022.09
5.5.330.0100.08724.40
5.5.320.0330.09324.40
5.5.310.0330.10024.26
5.5.300.0030.06022.07
5.5.290.0200.06322.05
5.5.280.0130.06024.94
5.5.270.0030.11025.01
5.5.260.0200.10724.91
5.5.250.0070.05724.75
5.5.240.0170.06724.21
5.4.450.0270.07323.50
5.4.440.0300.07723.25
5.4.430.0430.05723.59
5.4.420.0200.07023.63
5.4.410.0170.05323.48
5.4.400.0330.05723.05
5.4.390.0300.06323.15
5.4.380.0300.09322.59
5.4.370.0300.09322.90
5.4.360.0570.07323.07
5.4.350.0470.08022.82
5.4.340.0500.08322.71
5.4.320.0100.06216.64
5.4.310.0140.05916.63
5.4.300.0160.06916.63
5.4.290.0200.06816.63
5.4.280.0150.07216.53
5.4.270.0130.06116.53
5.4.260.0140.06016.53
5.4.250.0120.06916.54
5.4.240.0100.06416.53
5.4.230.0160.07316.52
5.4.220.0230.08716.52
5.4.210.0180.07116.52
5.4.200.0150.05716.52
5.4.190.0120.05916.52
5.4.180.0120.06316.52
5.4.170.0130.07616.52
5.4.160.0140.07616.52
5.4.150.0110.06916.52
5.4.140.0110.06516.20
5.4.130.0160.06216.19
5.4.120.0180.06316.15
5.4.110.0110.06516.15
5.4.100.0240.09416.15
5.4.90.0200.09916.14
5.4.80.0180.06616.14
5.4.70.0150.06016.14
5.4.60.0120.06616.14
5.4.50.0180.05816.14
5.4.40.0120.06716.13
5.4.30.0150.09216.12
5.4.20.0130.07016.13
5.4.10.0160.05916.12
5.4.00.0150.06215.61
5.3.290.0160.07916.98
5.3.280.0150.07116.91
5.3.270.0130.07516.92
5.3.260.0180.07316.92
5.3.250.0090.07316.92
5.3.240.0200.07716.92
5.3.230.0210.09516.91
5.3.220.0230.09916.89
5.3.210.0270.18016.89
5.3.200.0150.09516.88
5.3.190.0260.11616.88
5.3.180.0240.10516.88
5.3.170.0240.15916.88
5.3.160.0210.09716.88
5.3.150.0150.08916.88
5.3.140.0290.15616.87
5.3.130.0200.10516.86
5.3.120.0270.14016.86
5.3.110.0420.14616.95
5.3.100.0240.13316.45
5.3.90.0140.10016.34
5.3.80.0300.12116.32
5.3.70.0340.12616.32
5.3.60.0100.09316.30
5.3.50.0150.06916.26
5.3.40.0270.11516.26
5.3.30.0240.10516.22
5.3.20.0180.09216.00
5.3.10.0250.12716.07
5.3.00.0200.09615.95
5.2.170.0240.12413.23
5.2.160.0240.12713.23
5.2.150.0210.08813.23
5.2.140.0220.07013.23
5.2.130.0280.09613.19
5.2.120.0150.05813.19
5.2.110.0160.05913.19
5.2.100.0150.06613.19
5.2.90.0130.06813.18
5.2.80.0160.06413.17
5.2.70.0200.06713.17
5.2.60.0090.07213.13
5.2.50.0150.09013.10
5.2.40.0110.07213.08
5.2.30.0170.07812.88
5.2.20.0210.07813.14
5.2.10.0260.11012.78
5.2.00.0230.05712.64
5.1.60.0110.06813.46
5.1.50.0140.05913.46
5.1.40.0160.06613.45
5.1.30.0120.06013.79
5.1.20.0110.06813.82
5.1.10.0210.08013.46
5.1.00.0140.06913.46
5.0.50.0210.11412.03
5.0.40.0120.07911.89
5.0.30.0140.09711.70
5.0.20.0090.08211.67
5.0.10.0150.09411.64
5.0.00.0260.16011.52
4.4.90.0250.16610.10
4.4.80.0190.12610.05
4.4.70.0190.11210.05
4.4.60.0210.08810.08
4.4.50.0120.09410.06
4.4.40.0140.09810.06
4.4.30.0140.10210.05
4.4.20.0140.14510.12
4.4.10.0200.14210.12
4.4.00.0300.15510.08
4.3.110.0200.1289.99
4.3.100.0100.0939.98
4.3.90.0190.1489.96
4.3.80.0130.1529.88
4.3.70.0190.1679.95
4.3.60.0150.1579.96
4.3.50.0130.1149.95
4.3.40.0110.1589.91
4.3.30.0130.1378.72
4.3.20.0150.1398.70
4.3.10.0150.1568.65
4.3.00.0130.12316.97

preferences:
37.26 ms | 400 KiB | 5 Q