'; if ( $ms <= 0 ) { echo 0; } elseif ( $ms < 0.5 ) { echo '>0'; } else { echo (int)round( $ms ); } echo ' ms'; } function display_bench_results() { $html = ob_get_clean(); preg_match_all( '/>\{([^}]+)\}/is', $html, $matches ); $min = 0; $sum = 0; foreach ( $matches[1] as $i => $s ) { if ( $min <= 0 ) { $min = floatval( $s ); } else { $min = min( $min, floatval( $s ) ); } $sum += floatval( $s ); } foreach ( $matches[1] as $i => $s ) { $index = (int) round( floatval( $s ) * 100 / $min ); if ( $index > 5000 ) $class = 'no'; elseif ( $index > 500 ) $class = 'buggy'; elseif ( $index > 200 ) $class = 'incomplete'; elseif ( $index > 100 ) $class = 'almost'; else $class = 'yes'; $html = str_replace( $matches[0][$i], ' class="' . $class . '">' . $index, $html ); } echo $html; // echo 'Total: ' . round( $sum ) . ' ms'; } //------------------------------------------------------------------------------ function empty_array_bench( $method ) { $r = (int)round( $GLOBALS['repetitions'] / 5 ); $array = array(); bench(); $caption = $method( $array, $r ); $d = bench(); $sum = $d; echo '' . $caption . ''; ms( $d ); $array = range( 0, 100 ); bench(); $method( $array, $r ); $d = bench(); $sum += $d; ms( $d ); ms( $sum ); echo '{' . $sum . '}'; } function empty_array_method1( &$array, $r ) { while ( $r-- ) { $empty = count( $array ) === 0; } return 'count($array) === 0 //by reference'; } function empty_array_method2( $array, $r ) { while ( $r-- ) { $empty = count( $array ) === 0; } return 'count($array) === 0 //by value'; } function empty_array_method3( &$array, $r ) { while ( $r-- ) { $empty = $array === array(); } return '$array === []'; } function empty_array_method4( &$array, $r ) { while ( $r-- ) { $empty = empty( $array ); } return 'empty($array)'; } function empty_array_method5( &$array, $r ) { while ( $r-- ) { $notEmpty = (bool)$array; } return '(bool)$array'; } //------------------------------------------------------------------------------ function strcmp_bench( $method ) { $r = (int)round( $GLOBALS['repetitions'] ); bench(); $caption = $method( 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz', $r ); $d = bench(); $sum = $d; echo '' . $caption . ''; ms( $d ); bench(); $method( '0bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz', $r ); $d = bench(); $sum += $d; ms( $d ); bench(); $method( 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy0', $r ); $d = bench(); $sum += $d; ms( $d ); ms( $sum ); echo '{' . $sum . '}'; } function strcmp_method1( $var, $r ) { $equal = false; $b = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; while ( $r-- ) { $equal = $var == $b; } return '$a == $b'; } function strcmp_method2( $var, $r ) { $equal = false; $b = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; while ( $r-- ) { $equal = $var === $b; } return '$a === $b'; } function strcmp_method3( $var, $r ) { $equal = false; $b = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; while ( $r-- ) { $equal = !strcmp( $var, $b ); } return '!strcmp($a, $b)'; } function strcmp_method4( $var, $r ) { $equal = false; $b = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; while ( $r-- ) { $equal = strcmp( $var, $b ) == 0; } return 'strcmp($a, $b) == 0'; } function strcmp_method5( $var, $r ) { $equal = false; $b = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; while ( $r-- ) { $equal = strcmp( $var, $b ) === 0; } return 'strcmp($a, $b) === 0'; } function strcmp_method6( $var, $r ) { $equal = false; $b = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; while ( $r-- ) { $equal = strcasecmp( $var, $b ) === 0; } return 'strcasecmp($a, $b) === 0'; } //------------------------------------------------------------------------------ function array_get_bench( $method ) { $array = array(); for ( $i = 0; $i < 100; $i++ ) { $array[$i] = 'i' . $i; $array['key' . $i] = 's' . $i; } $r = (int)round( $GLOBALS['repetitions'] / 5 ); bench(); $caption = $method( $array, $r ); $d = bench(); echo '' . $caption . ''; ms( $d ); echo '{' . $d . '}'; } function array_get_method1( $array, $r ) { while ( $r-- ) { for ($i = 0; $i < 100; $i++) $result = $array[$i]; } return '$array[0]'; } function array_get_method2( $array, $r ) { while ( $r-- ) { for ($i = 0; $i < 100; $i++) $result = $array[$i]; } return '$array[\'key\']'; } //------------------------------------------------------------------------------ function empty_bench( $method ) { $r = (int)round( $GLOBALS['repetitions'] / 5 ); bench(); $caption = $method( -1, $r); $d = bench(); $sum = $d; echo '' . $caption . ''; ms( $d ); bench(); $method( null, $r ); $d = bench(); $sum += $d; ms( $d ); bench(); $method( false, $r ); $d = bench(); $sum += $d; ms( $d ); bench(); $method( '', $r ); $d = bench(); $sum += $d; ms( $d ); bench(); $method( '0', $r ); $d = bench(); $sum += $d; ms( $d ); bench(); $method( '1', $r ); $d = bench(); $sum += $d; ms( $d ); bench(); $method( 'x2345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890', $r ); $d = bench(); $sum += $d; ms( $d ); ms( $sum ); echo '{' . $sum . '}'; } function empty_method1( $var, $r ) { if ( $var < 0 ) { unset( $var ); } $isEmpty = false; while ( $r-- ) { if ( !$var ) { $isEmpty = true; } } // if ( empty( $var ) !== $isEmpty ) var_dump( $var, 'if (!$var)' ); return 'if (!$var)'; } function empty_method2( $var, $r ) { if ($var < 0) unset( $var ); $isEmpty = false; while ( $r-- ) { if ( empty( $var ) ) { $isEmpty = true; } } //if (empty( $var ) != $isEmpty) var_dump($var, 'if (empty($var))'); return 'if (empty($var))'; } function empty_method3( $var, $r ) { if ($var < 0) unset( $var ); $isEmpty = false; while ( $r-- ) { if ($var == '') { $isEmpty = true; } } //if (empty( $var ) != $isEmpty) var_dump($var, 'if ($var == \'\')'); return 'if ($var == \'\')'; } function empty_method4( $var, $r ) { if ($var < 0) unset( $var ); $isEmpty = false; while ( $r-- ) { if ('' == $var) { $isEmpty = true; } } //if (empty( $var ) != $isEmpty) var_dump($var, 'if (\'\' == $var)'); return 'if (\'\' == $var)'; } function empty_method5( $var, $r ) { if ($var < 0) unset( $var ); $isEmpty = false; while ( $r-- ) { if ($var === '') { $isEmpty = true; } } //if (empty( $var ) != $isEmpty) var_dump($var, 'if ($var === \'\')'); return 'if ($var === \'\')'; } function empty_method6( $var, $r ) { if ($var < 0) unset( $var ); $isEmpty = false; while ( $r-- ) { if ('' === $var) { $isEmpty = true; } } //if (empty( $var ) != $isEmpty) var_dump($var, 'if (\'\' === $var)'); return 'if (\'\' === $var)'; } function empty_method7( $var, $r ) { if ($var < 0) unset( $var ); $isEmpty = false; while ( $r-- ) { if (strcmp( $var, '' ) == 0) { $isEmpty = true; } } //if (empty( $var ) != $isEmpty) var_dump($var, 'if (strcmp($var, \'\') == 0)'); return 'if (strcmp($var, \'\') == 0)'; } function empty_method8( $var, $r ) { if ($var < 0) unset( $var ); $isEmpty = false; while ( $r-- ) { if (strcmp( '', $var ) == 0) { $isEmpty = true; } } //if (empty( $var ) != $isEmpty) var_dump($var, 'if (strcmp(\'\', $var) == 0)'); return 'if (strcmp(\'\', $var) == 0)'; } function empty_method9( $var, $r ) { if ($var < 0) unset( $var ); $isEmpty = false; while ( $r-- ) { if (strlen( $var ) == 0) { $isEmpty = true; } } //if (empty( $var ) != $isEmpty) var_dump($var, 'if (strlen($var) == 0)'); return 'if (strlen($var) == 0)'; } function empty_method10( $var, $r ) { if ($var < 0) unset( $var ); $isEmpty = false; while ( $r-- ) { if ( !strlen( $var ) ) { $isEmpty = true; } } //if (empty( $var ) != $isEmpty) var_dump($var, 'if (!strlen($var))'); return 'if (!strlen($var))'; } //------------------------------------------------------------------------------ function strstr_bench( $method, $needle = 'abcd' ) { $r = (int)round( $GLOBALS['repetitions'] / 30 ); $strLen = strstr( $method, 'strreplace_method' ) ? 50 : 1000; bench(); $caption = $method( str_repeat( 'x1234567890', $strLen ), $needle, $r ); $d = bench(); $sum = $d; echo '' . $caption . ''; ms( $d ); bench(); $method( 'abcd' . str_repeat( 'x1234567890', $strLen ), $needle, $r ); $d = bench(); $sum += $d; ms( $d ); bench(); $method( str_repeat( 'x1234567890', $strLen / 2 ) . 'abcd' . str_repeat( 'x1234567890', $strLen / 2 ), $needle, $r ); $d = bench(); $sum += $d; ms( $d ); bench(); $method( str_repeat( 'x1234567890', $strLen ) . 'abcd', $needle, $r ); $d = bench(); $sum += $d; ms( $d ); ms( $sum ); echo '{' . $sum . '}'; } function strstr_method1( $haystack, $needle, $r ) { $found = false; while ( $r-- ) { if ( strstr( $haystack, $needle ) ) { $found = true; } } return 'strstr($haystack, $needle)'; } function strstr_method2( $haystack, $needle, $r ) { $found = false; while ( $r-- ) { if (strpos( $haystack, $needle ) !== false) { $found = true; } } return 'strpos($haystack, $needle) !== false'; } function strstr_method3( $haystack, $needle, $r ) { $found = false; while ( $r-- ) { if (strstr( $haystack, $needle ) !== false) { $found = true; } } return 'strstr($haystack, $needle) !== false'; } function strstr_method4( $haystack, $needle, $r ) { $found = false; while ( $r-- ) { if ( stristr( $haystack, $needle ) ) { $found = true; } } return 'stristr($haystack, $needle)'; } function strstr_method5( $haystack, $needle, $r ) { $found = false; $regexp = '/' . preg_quote( $needle, '/' ) . '/'; while ( $r-- ) { if ( preg_match( $regexp, $haystack ) ) { $found = true; } } return 'preg_match("/$needle/", $haystack)'; } function strstr_method6( $haystack, $needle, $r ) { $found = false; $regexp = '/' . preg_quote( $needle, '/' ) . '/i'; while ( $r-- ) { if ( preg_match( $regexp, $haystack ) ) { $found = true; } } return 'preg_match("/$needle/i", $haystack)'; } function strstr_method7( $haystack, $needle, $r ) { $found = false; $regexp = '/' . preg_quote( $needle, '/' ) . '/S'; while ( $r-- ) { if ( preg_match( $regexp, $haystack ) ) { $found = true; } } return 'preg_match("/$needle/S", $haystack)'; } /* function strstr_method8( $haystack, $needle, $r ) { $found = false; while ( $r-- ) { if ( ereg( $needle, $haystack ) ) { $found = true; } } return 'ereg($needle, $haystack)'; } */ //------------------------------------------------------------------------------ function startsWith_method1( $haystack, $needle, $r ) { while ( $r-- ) { $result = $haystack[0] === $needle; } return '$haystack[0] === \'n\''; } function startsWith_method2( $haystack, $needle, $r ) { while ( $r-- ) { $result = strncmp( $haystack, $needle, strlen( $needle ) ) === 0; } return 'strncmp($haystack, $needle, strlen($needle)) === 0'; } function startsWith_method3( $haystack, $needle, $r ) { while ( $r-- ) { $result = strncmp( $haystack, $needle, 1 ) === 0; } return 'strncmp($haystack, \'needle\', 6) === 0'; } function startsWith_method4( $haystack, $needle, $r ) { while ( $r-- ) { $result = strncasecmp( $haystack, $needle, strlen( $needle ) ) === 0; } return 'strncasecmp($haystack, $needle, strlen($needle)) === 0'; } function startsWith_method5( $haystack, $needle, $r ) { while ( $r-- ) { $result = strpos( $haystack, $needle ) === 0; } return 'strpos($haystack, $needle) === 0'; } function startsWith_method6( $haystack, $needle, $r ) { while ( $r-- ) { $result = substr( $haystack, 0, strlen( $needle ) ) === $needle; } return 'substr($haystack, 0, strlen($needle)) === $needle'; } function startsWith_method7( $haystack, $needle, $r ) { while ( $r-- ) { $result = strcmp( substr( $haystack, 0, strlen( $needle ) ), $needle ) === 0; } return 'strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0'; } function startsWith_method8( $haystack, $needle, $r ) { while ( $r-- ) { $result = substr_compare( $haystack, $needle, 0, strlen( $needle ) ); } return 'substr_compare($haystack, $needle, 0, strlen($needle))'; } function startsWith_method9( $haystack, $needle, $r ) { while ( $r-- ) { $result = substr_compare( $haystack, $needle, 0 ); } return 'substr_compare($haystack, $needle, 0)'; } function startsWith_method10( $haystack, $needle, $r ) { while ( $r-- ) { $result = preg_match( '/^' . preg_quote( $needle, '/' ) . '/', $haystack ); } return 'preg_match(\'/^\' . preg_quote($needle, \'/\') . \'/\', $haystack)'; } //------------------------------------------------------------------------------ function endsWith_method1( $haystack, $needle, $r ) { while ( $r-- ) { $result = $haystack[strlen( $haystack ) - 1] === $needle; } return '$haystack[strlen($haystack) - 1] === \'n\''; } function endsWith_method2( $haystack, $needle, $r ) { while ( $r-- ) { $result = substr( $haystack, strlen( $haystack ) - strlen( $needle ) ) === $needle; } return 'substr($haystack, strlen($haystack) - strlen($needle)) === $needle'; } function endsWith_method3( $haystack, $needle, $r ) { while ( $r-- ) { $result = substr( $haystack, -strlen( $needle ) ) === $needle; } return 'substr($haystack, -strlen($needle)) === $needle'; } function endsWith_method4( $haystack, $needle, $r ) { while ( $r-- ) { $result = substr( $haystack, -1 ) === $needle; } return 'substr($haystack, -1) === \'n\''; } function endsWith_method5( $haystack, $needle, $r ) { while ( $r-- ) { $result = strcmp( substr( $haystack, -strlen( $needle ) ), $needle) === 0; } return 'strcmp(substr($haystack, -strlen($needle)), $needle) === 0'; } function endsWith_method6( $haystack, $needle, $r ) { while ( $r-- ) { $result = substr_compare( $haystack, $needle, -strlen( $needle ), strlen( $needle ) ); } return 'substr_compare($haystack, $needle, -strlen($needle), strlen($needle))'; } function endsWith_method7( $haystack, $needle, $r ) { while ( $r-- ) { $result = substr_compare( $haystack, $needle, -strlen( $needle ) ); } return 'substr_compare($haystack, $needle, -strlen($needle))'; } function endsWith_method8( $haystack, $needle, $r ) { while ( $r-- ) { $result = preg_match( '/' . preg_quote( $needle, '/' ) . '$/', $haystack ); } return 'preg_match(\'/\' . preg_quote($needle, \'/\') . \'$/\', $haystack)'; } //------------------------------------------------------------------------------ function strreplace_method1( $subject, $search, $r ) { $replace = $search; while ( $r-- ) { $result = str_replace( $search, $replace, $subject ); } return 'str_replace($search, $replace, $subject)'; } function strreplace_method2( $subject, $search, $r ) { $replace = $search; $regexp = '/' . preg_quote( $search, '/' ) . '/'; while ( $r-- ) { $result = preg_replace( $regexp, $replace, $subject ); } return 'preg_replace("/$search/", $replace, $subject)'; } function strreplace_method3( $subject, $search, $r ) { $replace = $search; $regexp = '/' . preg_quote( $search, '/' ) . '/S'; while ( $r-- ) { $result = preg_replace( $regexp, $replace, $subject ); } return 'preg_replace("/$search/S", $replace, $subject)'; } /* function strreplace_method4( $subject, $search, $r ) { $replace = $search; while ( $r-- ) { $result = ereg_replace( $search, $replace, $subject ); } return 'ereg_replace($search, $replace, $subject)'; } */ function strreplace_method4( $subject, $search, $r ) { $replace = $search; $a = array( $search => $replace ); while ( $r-- ) { $result = strtr( $subject, $a ); } return 'strtr($subject, array($search => $replace))'; } //------------------------------------------------------------------------------ function charreplace_method1( $subject, $search, $r ) { $fromChar = 'a'; $toChar = 'b'; while ( $r-- ) { $result = str_replace( $fromChar, $toChar, $subject ); } return 'str_replace($fromChar, $toChar, $subject)'; } function charreplace_method2( $subject, $search, $r ) { $fromChar = 'a'; $toChar = 'b'; while ( $r-- ) { $result = strtr( $subject, $fromChar, $toChar ); } return 'strtr($subject, $fromChar, $toChar)'; } function charreplace_method3( $subject, $search, $r ) { $a = array( 'a' => 'b' ); while ( $r-- ) { $result = strtr( $subject, $a ); } return 'strtr($subject, array($fromChar => $toChar))'; } //------------------------------------------------------------------------------ function trim_bench( $method ) { $r = (int)round( $GLOBALS['repetitions'] / 50 ); bench(); $caption = $method( "x234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890", $r); $d = bench(); $sum = $d; echo '' . $caption . ''; ms( $d ); bench(); $method( ",,,,,,,,,,,,,,,,,,,,x234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890", $r); $d = bench(); $sum += $d; ms( $d ); bench(); $method( "x234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890,,,,,,,,,,,,,,,,,,,,", $r); $d = bench(); $sum += $d; ms( $d ); bench(); $method( ",,,,,,,,,,,,,,,,,,,,x234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890,,,,,,,,,,,,,,,,,,,,", $r); $d = bench(); $sum += $d; ms( $d ); ms( $sum ); echo '{' . $sum . '}'; } function trim_method1( $string, $r ) { while ( $r-- ) { $string = trim( $string, ',' ); } return 'trim($string, \',\')'; } function trim_method2( $string, $r ) { while ( $r-- ) { $string = preg_replace( '/^,*|,*$/', '', $string ); } return 'preg_replace(\'/^,*|,*$/\', \'\', $string)'; } function trim_method3( $string, $r ) { while ( $r-- ) { $string = preg_replace( '/^,*|,*$/m', '', $string ); } return 'preg_replace(\'/^,*|,*$/m\', \'\', $string)'; } function trim_method4( $string, $r ) { while ( $r-- ) { $string = preg_replace( '/^,+|,+$/', '', $string ); } return 'preg_replace(\'/^,+|,+$/\', \'\', $string)'; } function trim_method5( $string, $r ) { while ( $r-- ) { $string = preg_replace( '/^,+|,+$/m', '', $string ); } return 'preg_replace(\'/^,+|,+$/m\', \'\', $string)'; } function trim_method6( $string, $r ) { while ( $r-- ) { $string = preg_replace( '/^,+/', '', preg_replace( '/,+$/', '', $string ) ); } return 'preg_replace(\'/^,+/\', \'\', preg_replace(\'/,+$/\', \'\', …))'; } //------------------------------------------------------------------------------ function split_bench( $method ) { $r = (int)round( $GLOBALS['repetitions'] / 10 ); bench(); $caption = $method( '', $r); $d = bench(); $sum = $d; echo '' . $caption . ''; ms( $d ); bench(); $method( 'x234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890', $r); $d = bench(); $sum += $d; ms( $d ); bench(); $method( 'x2345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890,12345678901234567890', $r); $d = bench(); $sum += $d; ms( $d ); ms( $sum ); echo '{' . $sum . '}'; } function split_method1( $string, $r ) { while ( $r-- ) { $array = explode( ',', $string ); } return 'explode(\',\', $string)'; } /* function split_method2( $string, $r ) { while ( $r-- ) { $array = split( ',', $string ); } return 'split(\',\', $string)'; } */ function split_method2( $string, $r ) { while ( $r-- ) { $array = preg_split( '/,/', $string ); } return 'preg_split(\'/,/\', $string)'; } function split_method3( $string, $r ) { while ( $r-- ) { preg_match_all( '/[^,]+/', $string, $matches ); $array = $matches[0]; } return 'preg_match_all(\'/[^,]+/\', $string, $matches)'; } //------------------------------------------------------------------------------ function loop_bench( $method ) { $array = array(); $i = 128; while ( $i-- ) { $array[] = 'abcd'; } reset( $array ); $r = (int)round( $GLOBALS['repetitions'] / 200 ); bench(); $caption = $method( $array, $r ); $sum = bench(); echo '' . $caption . ''; ms( $sum ); echo '{' . $sum . '}'; } function loop_method1( &$array, $r ) { $found = false; while ( $r-- ) { for ($i = 0; $i < count( $array ); $i++) { $found = true; } } return 'for ($i = 0; $i < count($array); $i++) //by reference'; } function loop_method2( $array, $r ) { $found = false; while ( $r-- ) { for ($i = 0; $i < count( $array ); $i++) { $found = true; } } return 'for ($i = 0; $i < count($array); $i++) //by value'; } function loop_method3( &$array, $r ) { $found = false; while ( $r-- ) { for ($i = 0, $count = count( $array ); $i < $count; $i++) { $found = true; } } return 'for ($i = 0, $count = count($array); $i < $count; $i++)'; } function loop_method4( &$array, $r ) { $found = false; while ( $r-- ) { for ($i = count( $array ) - 1; $i >= 0; $i--) { $found = true; } } return 'for ($i = count($array) - 1; $i >= 0; $i--)'; } function loop_method5( &$array, $r ) { $found = false; while ( $r-- ) { for ($i = count( $array ) - 1; $i >= 0; --$i) { $found = true; } } return 'for ($i = count($array) - 1; $i >= 0; --$i)'; } function loop_method6( &$array, $r ) { $found = false; while ( $r-- ) { $i = count( $array ); while ( $i-- ) { $found = true; } } return '$i = count($array); while ($i--)'; } //------------------------------------------------------------------------------ function concat_bench( $method ) { $r = (int)round( $GLOBALS['repetitions'] ); bench(); $caption = $method( $r ); $sum = bench(); echo '' . $caption . ''; ms( $sum ); echo '{' . $sum . '}'; } function concat_method1( $r ) { $array = array( 'mediumLengthExampleString', 'mediumLengthExampleString', 'mediumLengthExampleString' ); while ( $r-- ) { $string = implode( ' ', $array ); } return 'implode(\' \', $array)'; } function concat_method2( $r ) { $array = array( 'mediumLengthExampleString', 'mediumLengthExampleString', 'mediumLengthExampleString' ); while ( $r-- ) { $string = "$array[0] $array[1] $array[2]"; } return '"$array[0] $array[1] $array[2]"'; } function concat_method3( $r ) { $array = array( 'mediumLengthExampleString', 'mediumLengthExampleString', 'mediumLengthExampleString' ); while ( $r-- ) { $string = $array[0] . ' ' . $array[1] . ' ' . $array[2]; } return '$array[0] . \' \' . $array[1] . \' \' . $array[2]'; } function concat_method4( $r ) { $array = array( 'mediumLengthExampleString', 'mediumLengthExampleString', 'mediumLengthExampleString' ); while ( $r-- ) { $string = sprintf( '%s %s %s', $array[0], $array[1], $array[2] ); } return 'sprintf(\'%s %s %s\', $array[0], $array[1], $array[2])'; } function concat_method5( $r ) { $array = array( 'mediumLengthExampleString', 'mediumLengthExampleString', 'mediumLengthExampleString' ); while ( $r-- ) { $string = vsprintf( '%s %s %s', $array ); } return 'vsprintf(\'%s %s %s\', $array)'; } //------------------------------------------------------------------------------ function quotes_bench( $method ) { $r = (int)round( $GLOBALS['repetitions'] ); bench(); $caption = $method( $r ); $sum = bench(); echo '' . $caption . ''; ms( $sum ); echo '{' . $sum . '}'; } function quotes_method1( $r ) { while ( $r-- ) { $string = 'x2345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678'; } return '\'contains no dollar signs\''; } function quotes_method2( $r ) { while ( $r-- ) { $string = "x2345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678"; } return '"contains no dollar signs"'; } function quotes_method3( $r ) { while ( $r-- ) { $string = 'x234567890 $a 1234567890 $a 1234567890 $a 1234567890 $a 1234567890 $a 1234567890 $a 1234567890 $a 1234567890 $a 1234567890 $a 12'; } return '\'$variables $are $not $replaced\''; } function quotes_method4( $r ) { while ( $r-- ) { $string = "x234567890 \$a 1234567890 \$a 1234567890 \$a 1234567890 \$a 1234567890 \$a 1234567890 \$a 1234567890 \$a 1234567890 \$a 1234567890 \$a 12"; } return '"\\$variables \\$are \\$not \\$replaced"'; } function quotes_method5( $r ) { $a = '$a'; while ( $r-- ) { $string = "x234567890 $a 1234567890 $a 1234567890 $a 1234567890 $a 1234567890 $a 1234567890 $a 1234567890 $a 1234567890 $a 1234567890 $a 12"; } return '"$variables $are $replaced"'; } function quotes_method6( $r ) { $a = '$a'; while ( $r-- ) { $string = 'x234567890 ' . $a . ' 1234567890 ' . $a . ' 1234567890 ' . $a . ' 1234567890 ' . $a . ' 1234567890 ' . $a . ' 1234567890 ' . $a . ' 1234567890 ' . $a . ' 1234567890 ' . $a . ' 1234567890 ' . $a . ' 12'; } return '$variables . \' \' . $are . \' \' . $replaced'; } function quotes_method7( $r ) { $a = '$a'; while ( $r-- ) { $string = "x234567890 " . $a . " 1234567890 " . $a . " 1234567890 " . $a . " 1234567890 " . $a . " 1234567890 " . $a . " 1234567890 " . $a . " 1234567890 " . $a . " 1234567890 " . $a . " 1234567890 " . $a . " 12"; } return '$variables . " " . $are . " " . $replaced'; } //------------------------------------------------------------------------------ ?> My Collection of PHP Performance Benchmarks

My PHP Performance Benchmarks

PHP version is running on this server. The benchmarks are done live. Reload the page to get fresh numbers. You are free to use the source for whatever you want. Giving credits to me (Thiemo Mättig) would be nice.

Please note that these are micro benchmarks. Micro benchmarks are stupid. I created this comparison to learn something about PHP and how the PHP compiler works. This can not be used to compare PHP versions or servers.

Check if a String is empty

Method Undefined Null False Empty string String '0' String '1' Long string Summary Index

My conclusion: In most cases, Do not use empty() because it does not trigger a warning when used with undefined variables. Note that empty('0') returns true. Use strlen() if you want to detect '0'. Try to avoid == at all because it may cause strange behaviour (e.g. '9a' == 9 returns true). Prefer === over == and !== over != if possible because it does compare the variable types in addition to the contents.

Check if an Array is empty

Method Empty array 100 elements Summary Index

My conclusion: Why count if you don't care about the exact number?

Compare two Strings

Method Equal First character not equal Last character not equal Summary Index

My conclusion: Use what fits your needs.

Check if a String contains another String

Method Not found Found at the start Found in the middle Found at the end Summary Index

My conclusion: It does not matter if you use strstr() or strpos(). Use the preg…() functions only if you need the power of regular expressions. Never use the ereg…() functions.

Check if a String starts with another String

Method Not found Found at the start Found in the middle Found at the end Summary Index

My conclusion: strpos() is very fast and can be used in almost all cases. strncmp() is good if you are looking for a constant length needle.

Check if a String ends with another String

Method Not found Found at the start Found in the middle Found at the end Summary Index

My conclusion: Using substr() with a negative position is a good trick.

Replace a (>1 Character) String inside another String

Method Not found Found at the start Found in the middle Found at the end Summary Index

My conclusion: Never use the ereg…() functions.

Replace a Character inside a String

Method Not found Found at the start Found in the middle Found at the end Summary Index

My conclusion: Since PHP 7.0 strtr() can sometimes beat str_replace().

Trim Characters from the Beginning and End of a String

Method Not found Found at start Found at end Found at both sides Summary Index

My conclusion: Always benchmark your regular expressions! In this case, with .* you also replace nothing with nothing which takes time because there is a lot of “nothing” in every string.

Split a String into an Array

Method Empty string Single occurrence Multiple occurrences Summary Index

My conclusion: Don't use split(). It got deprecated in PHP 5.3 and removed from PHP 7.0.

Loop a numerical indexed Array of Strings

Method Summary Index

My conclusion: count() could have been horribly slow in PHP 5 and below when copy-on-write accidentally kicked in. Always precalculate it, if possible.

Get Elements from an Array

Method Summary Index

My conclusion: I like associative arrays.

Implode an Array

Method Summary Index

My conclusion: String concatenation is a cheap operation in PHP. Don't waste your time benchmarking this.

The single vs. double Quotes Myth

Method Summary Index

My conclusion: It does not matter if you use single or double quotes at all. The inclusion of variables has a measurable effect, but that's independent from the quotes.

© Thiemo Mättig, created in September 2008, updated in August 2017
More PHP experiments »