|
<?php |
|
/** |
|
* Consolidated String Comparison Benchmark |
|
* |
|
* Usage: php benchmark.php <method> |
|
* |
|
* Methods: |
|
* char-by-char Character-by-character (case-sensitive) |
|
* substr-compare substr_compare() (case-sensitive) |
|
* substr-equals substr() with === (case-sensitive) |
|
* char-by-char-ci Character-by-character (case-insensitive) |
|
* substr-compare-ci substr_compare() with case-insensitive flag |
|
* strncasecmp strncasecmp() (case-insensitive) |
|
* strtolower-substr strtolower(substr()) === (case-insensitive) |
|
*/ |
|
|
|
// Configuration. |
|
$config = array( |
|
'case_sensitive' => array( |
|
'test_string' => str_repeat( 'Lorem ipsum SYSTEM dolor sit amet SYSTEM consectetur SYSTEM adipiscing elit ', 1000 ), |
|
'iterations' => 1000, |
|
), |
|
'case_insensitive' => array( |
|
'test_string' => str_repeat( 'Lorem ipsum SYSTEM dolor sit SyStEm amet system consectetur SySteM adipiscing elit ', 1000 ), |
|
'iterations' => 1000, |
|
), |
|
); |
|
|
|
// Benchmark methods. |
|
$methods = array( |
|
'char-by-char' => array( |
|
'name' => 'Character-by-character comparison (case-sensitive)', |
|
'case_sensitive' => true, |
|
'function' => function ( $test_string, $iterations ) { |
|
$matches = 0; |
|
for ( $i = 0; $i < $iterations; $i++ ) { |
|
for ( $at = 0; $at < strlen( $test_string ) - 6; $at++ ) { |
|
if ( |
|
'S' === $test_string[ $at ] && |
|
'Y' === $test_string[ $at + 1 ] && |
|
'S' === $test_string[ $at + 2 ] && |
|
'T' === $test_string[ $at + 3 ] && |
|
'E' === $test_string[ $at + 4 ] && |
|
'M' === $test_string[ $at + 5 ] |
|
) { |
|
++$matches; |
|
} |
|
} |
|
} |
|
return $matches; |
|
}, |
|
), |
|
'substr-compare' => array( |
|
'name' => 'substr_compare() (case-sensitive)', |
|
'case_sensitive' => true, |
|
'function' => function ( $test_string, $iterations ) { |
|
$matches = 0; |
|
for ( $i = 0; $i < $iterations; $i++ ) { |
|
for ( $at = 0; $at < strlen( $test_string ) - 6; $at++ ) { |
|
if ( 0 === substr_compare( $test_string, 'SYSTEM', $at, 6 ) ) { |
|
++$matches; |
|
} |
|
} |
|
} |
|
return $matches; |
|
}, |
|
), |
|
'substr-equals' => array( |
|
'name' => 'substr() with === comparison (case-sensitive)', |
|
'case_sensitive' => true, |
|
'function' => function ( $test_string, $iterations ) { |
|
$matches = 0; |
|
for ( $i = 0; $i < $iterations; $i++ ) { |
|
for ( $at = 0; $at < strlen( $test_string ) - 6; $at++ ) { |
|
if ( 'SYSTEM' === substr( $test_string, $at, 6 ) ) { |
|
++$matches; |
|
} |
|
} |
|
} |
|
return $matches; |
|
}, |
|
), |
|
'char-by-char-ci' => array( |
|
'name' => 'Character-by-character comparison (case-insensitive)', |
|
'case_sensitive' => false, |
|
'function' => function ( $test_string, $iterations ) { |
|
$matches = 0; |
|
for ( $i = 0; $i < $iterations; $i++ ) { |
|
for ( $at = 0; $at < strlen( $test_string ) - 6; $at++ ) { |
|
if ( |
|
( 'S' === $test_string[ $at ] || 's' === $test_string[ $at ] ) && |
|
( 'Y' === $test_string[ $at + 1 ] || 'y' === $test_string[ $at + 1 ] ) && |
|
( 'S' === $test_string[ $at + 2 ] || 's' === $test_string[ $at + 2 ] ) && |
|
( 'T' === $test_string[ $at + 3 ] || 't' === $test_string[ $at + 3 ] ) && |
|
( 'E' === $test_string[ $at + 4 ] || 'e' === $test_string[ $at + 4 ] ) && |
|
( 'M' === $test_string[ $at + 5 ] || 'm' === $test_string[ $at + 5 ] ) |
|
) { |
|
++$matches; |
|
} |
|
} |
|
} |
|
return $matches; |
|
}, |
|
), |
|
'substr-compare-ci' => array( |
|
'name' => 'substr_compare() with case-insensitive flag', |
|
'case_sensitive' => false, |
|
'function' => function ( $test_string, $iterations ) { |
|
$matches = 0; |
|
for ( $i = 0; $i < $iterations; $i++ ) { |
|
for ( $at = 0; $at < strlen( $test_string ) - 6; $at++ ) { |
|
if ( 0 === substr_compare( $test_string, 'SYSTEM', $at, 6, true ) ) { |
|
++$matches; |
|
} |
|
} |
|
} |
|
return $matches; |
|
}, |
|
), |
|
'strncasecmp' => array( |
|
'name' => 'strncasecmp() (case-insensitive)', |
|
'case_sensitive' => false, |
|
'function' => function ( $test_string, $iterations ) { |
|
$matches = 0; |
|
for ( $i = 0; $i < $iterations; $i++ ) { |
|
for ( $at = 0; $at < strlen( $test_string ) - 6; $at++ ) { |
|
if ( 0 === strncasecmp( substr( $test_string, $at ), 'SYSTEM', 6 ) ) { |
|
++$matches; |
|
} |
|
} |
|
} |
|
return $matches; |
|
}, |
|
), |
|
'strtolower-substr' => array( |
|
'name' => 'strtolower(substr()) === (case-insensitive)', |
|
'case_sensitive' => false, |
|
'function' => function ( $test_string, $iterations ) { |
|
$matches = 0; |
|
for ( $i = 0; $i < $iterations; $i++ ) { |
|
for ( $at = 0; $at < strlen( $test_string ) - 6; $at++ ) { |
|
if ( 'system' === strtolower( substr( $test_string, $at, 6 ) ) ) { |
|
++$matches; |
|
} |
|
} |
|
} |
|
return $matches; |
|
}, |
|
), |
|
); |
|
|
|
// Parse CLI arguments. |
|
if ( $argc < 2 ) { |
|
echo "Usage: php benchmark.php <method>\n\n"; |
|
echo "Available methods:\n"; |
|
foreach ( $methods as $key => $method ) { |
|
$type = $method['case_sensitive'] ? 'case-sensitive' : 'case-insensitive'; |
|
echo " {$key}\n {$method['name']} ({$type})\n"; |
|
} |
|
exit( 1 ); |
|
} |
|
|
|
$selected_method = $argv[1]; |
|
|
|
if ( ! isset( $methods[ $selected_method ] ) ) { |
|
echo "Error: Unknown method '{$selected_method}'\n"; |
|
echo "Run 'php benchmark.php' to see available methods.\n"; |
|
exit( 1 ); |
|
} |
|
|
|
// Get configuration for this method. |
|
$method = $methods[ $selected_method ]; |
|
$test_config = $method['case_sensitive'] ? $config['case_sensitive'] : $config['case_insensitive']; |
|
$test_string = $test_config['test_string']; |
|
$iterations = $test_config['iterations']; |
|
|
|
// Display test information. |
|
echo "Benchmarking: {$method['name']}\n"; |
|
echo 'String length: ' . number_format( strlen( $test_string ) ) . " bytes\n"; |
|
echo 'Iterations: ' . number_format( $iterations ) . "\n"; |
|
|
|
// Run benchmark. |
|
$start = microtime( true ); |
|
$matches = $method['function']( $test_string, $iterations ); |
|
$elapsed = microtime( true ) - $start; |
|
|
|
// Display results. |
|
echo "\nTime: " . number_format( $elapsed, 2 ) . " seconds\n"; |
|
echo 'Matches found: ' . number_format( $matches ) . "\n"; |
|
echo "--------------------------------\n"; |