3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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()); // Run the substr benchmark // exhaust the gc's threshhold at 10K $it = 10000; 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']); // compare the results $a = array_sum($results['substr']) / $it; $b = array_sum($results['strpos']) / $it; printf("substr: %.6fs/iteration @ %d iterations\nstrpos: %.6fs/iteration @ %d iterations\n\n", $a, $it, $b, $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"); }
Output for 7.1.0
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations substr is 99% faster than strpos
Output for 7.0.14
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 93% faster than substr
Output for 7.0.10
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 90% faster than substr
Output for 7.0.2, 7.0.9
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 84% faster than substr
Output for 7.0.8
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 87% faster than substr
Output for 7.0.7
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 86% faster than substr
Output for 7.0.6
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 72% faster than substr
Output for 5.5.18, 7.0.5
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 78% faster than substr
Output for 5.3.19, 7.0.4
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 80% faster than substr
Output for 5.5.8, 7.0.3
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 82% faster than substr
Output for 5.4.15, 5.4.18, 7.0.1
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 73% faster than substr
Output for 7.0.0
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 85% faster than substr
Output for hhvm-3.12.0
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 84% faster than substr
Output for hhvm-3.10.0
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 90% faster than substr
Output for 5.0.2, 5.3.6, 5.3.14 - 5.3.15, 5.6.28
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 67% faster than substr
Output for 5.3.7, 5.3.10, 5.3.17, 5.3.27, 5.5.28, 5.6.25
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 69% faster than substr
Output for 5.5.38, 5.6.24
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 66% faster than substr
Output for 5.5.33, 5.6.23
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 54% faster than substr
Output for 5.3.2, 5.5.25, 5.6.22
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 64% faster than substr
Output for 5.6.21
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 56% faster than substr
Output for 5.5.32, 5.6.20
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 53% faster than substr
Output for 5.3.11, 5.4.1, 5.5.34, 5.6.12, 5.6.19
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 66% faster than substr
Output for 5.3.21, 5.3.23, 5.4.10, 5.4.26, 5.4.31, 5.5.7, 5.5.20, 5.6.2, 5.6.5, 5.6.7, 5.6.16, 5.6.18
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 70% faster than substr
Output for 5.1.2, 5.6.17
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 72% faster than substr
Output for 5.3.8, 5.4.8, 5.5.13, 5.6.15
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 70% faster than substr
Output for 5.6.14
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 65% faster than substr
Output for 5.4.40, 5.4.42, 5.5.14, 5.5.36, 5.6.11, 5.6.13
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 67% faster than substr
Output for 5.2.14, 5.3.16, 5.3.29, 5.4.3, 5.4.41, 5.5.16, 5.5.22, 5.5.24, 5.5.30, 5.5.35, 5.6.9 - 5.6.10
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 68% faster than substr
Output for 5.6.8
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 40% faster than substr
Output for 5.4.13, 5.6.6
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 63% faster than substr
Output for 5.3.25, 5.4.9, 5.4.12, 5.4.16, 5.4.23, 5.4.28, 5.4.34, 5.4.39, 5.5.5, 5.5.21, 5.6.4
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 68% faster than substr
Output for 5.3.20, 5.4.19, 5.4.29 - 5.4.30, 5.5.0, 5.6.3
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 69% faster than substr
Output for 5.6.1
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 96% faster than substr
Output for 5.4.7, 5.4.21, 5.4.37 - 5.4.38, 5.5.1, 5.5.10, 5.5.12, 5.5.23, 5.6.0
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 71% faster than substr
Output for 5.4.25, 5.5.26, 5.5.37
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 47% faster than substr
Output for 5.4.44, 5.5.31
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 55% faster than substr
Output for 5.4.27, 5.5.29
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 52% faster than substr
Output for 5.5.27
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 60% faster than substr
Output for 5.5.19
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 65% faster than substr
Output for 5.3.18, 5.3.26, 5.3.28, 5.5.6, 5.5.15
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 77% faster than substr
Output for 5.5.9, 5.5.11
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 61% faster than substr
Output for 5.4.14, 5.5.4
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 74% faster than substr
Output for 5.3.22, 5.4.20, 5.4.35, 5.5.2 - 5.5.3
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 67% faster than substr
Output for 5.4.45
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 64% faster than substr
Output for 5.4.43
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 32% faster than substr
Output for 5.4.36
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 76% faster than substr
Output for 5.3.13, 5.4.32
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 57% faster than substr
Output for 5.4.24
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 75% faster than substr
Output for 5.4.22
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 79% faster than substr
Output for 5.4.17
substr: 0.000000s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 64% faster than substr
Output for 5.4.11
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 65% faster than substr
Output for 5.2.2, 5.4.6
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 75% faster than substr
Output for 5.4.5
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 45% faster than substr
Output for 5.4.4
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 55% faster than substr
Output for 5.2.9, 5.4.2
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 62% faster than substr
Output for 5.4.0
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 42% faster than substr
Output for 5.3.12, 5.3.24
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 63% faster than substr
Output for 5.3.0, 5.3.9
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 68% faster than substr
Output for 5.2.6, 5.3.5
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 70% faster than substr
Output for 5.3.4
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 71% faster than substr
Output for 5.3.3
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 80% faster than substr
Output for 5.3.1
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 35% faster than substr
Output for 5.2.8, 5.2.17
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 73% faster than substr
Output for 5.2.16
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 76% faster than substr
Output for 5.2.15
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 56% faster than substr
Output for 4.4.9, 5.1.4, 5.2.7, 5.2.13
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 72% faster than substr
Output for 5.2.11 - 5.2.12
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 78% faster than substr
Output for 5.2.10
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 81% faster than substr
Output for 5.2.5
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 61% faster than substr
Output for 5.2.3 - 5.2.4
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 71% faster than substr
Output for 5.2.1
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000000s/iteration @ 10000 iterations strpos is 87% faster than substr
Output for 5.1.6 - 5.2.0
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 66% faster than substr
Output for 5.1.5
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 76% faster than substr
Output for 5.1.3
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 45% faster than substr
Output for 5.1.1
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 46% faster than substr
Output for 5.1.0
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 75% faster than substr
Output for 5.0.0, 5.0.5
substr: 0.000002s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 61% faster than substr
Output for 5.0.4
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 79% faster than substr
Output for 5.0.3
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 74% faster than substr
Output for 5.0.1
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 59% faster than substr
Output for 4.4.8
substr: 0.000002s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 71% faster than substr
Output for 4.4.1, 4.4.7
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 80% faster than substr
Output for 4.4.6
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 83% faster than substr
Output for 4.4.5
substr: 0.000002s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 64% faster than substr
Output for 4.4.4
substr: 0.000002s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 78% faster than substr
Output for 4.3.4, 4.4.3
substr: 0.000002s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 65% faster than substr
Output for 4.4.2
substr: 0.000002s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 72% faster than substr
Output for 4.4.0
substr: 0.000002s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 47% faster than substr
Output for 4.3.11
substr: 0.000002s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 70% faster than substr
Output for 4.3.9 - 4.3.10
substr: 0.000001s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 82% faster than substr
Output for 4.3.8
substr: 0.000002s/iteration @ 10000 iterations strpos: 0.000002s/iteration @ 10000 iterations strpos is 78% faster than substr
Output for 4.3.7
substr: 0.000002s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 69% faster than substr
Output for 4.3.6
substr: 0.000002s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 83% faster than substr
Output for 4.3.3, 4.3.5
substr: 0.000002s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 62% faster than substr
Output for 4.3.2
substr: 0.000002s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 66% faster than substr
Output for 4.3.1
substr: 0.000002s/iteration @ 10000 iterations strpos: 0.000001s/iteration @ 10000 iterations strpos is 63% faster than substr
Output for 4.3.0
substr: 0.000002s/iteration @ 10000 iterations strpos: 0.000002s/iteration @ 10000 iterations strpos is 86% faster than substr