| 1 |
<?php |
| 2 |
require_once __DIR__ . '/../src/MultiStringMatcher.php'; |
| 3 |
require_once __DIR__ . '/../src/MultiStringReplacer.php'; |
| 4 |
|
| 5 |
use AhoCorasick\MultiStringReplacer; |
| 6 |
|
| 7 |
if ( !file_exists( __DIR__ . '/23835-0.txt' ) ) { |
| 8 |
die( "Please download http://www.gutenberg.org/files/23835/23835-0.txt\n" ); |
| 9 |
} |
| 10 |
|
| 11 |
if ( !file_exists( __DIR__ . '/ZhConversion.php' ) ) { |
| 12 |
die( "You need ZhConversion.php, from " . |
| 13 |
"https://github.com/wikimedia/mediawiki/blob/master/includes/ZhConversion.php\n" ); |
| 14 |
} |
| 15 |
|
| 16 |
require_once __DIR__ . '/ZhConversion.php'; |
| 17 |
|
| 18 |
$text = file_get_contents( __DIR__ . '/23835-0.txt' ); |
| 19 |
|
| 20 |
$options = getopt( '', [ 'count:', 'input:', 'profile', 'fss', 'msr', 'strtr' ] ); |
| 21 |
$text = file_get_contents( isset( $options['input'] ) ? $options['input'] : 'SueiTangYanYi.txt' ); |
| 22 |
$loops = isset( $options['count'] ) ? intval( $options['count'] ) : 5; |
| 23 |
if ( !isset( $options['fss'] ) && !isset( $options['msr'] ) && !isset( $options['strtr'] ) ) { |
| 24 |
$options['fss'] = true; |
| 25 |
$options['msr'] = true; |
| 26 |
$options['strtr'] = true; |
| 27 |
} |
| 28 |
$profile = false; |
| 29 |
if ( isset( $options['profile'] ) ) { |
| 30 |
$profile = true; |
| 31 |
$options['msr'] = true; |
| 32 |
unset( $options['fss'] ); |
| 33 |
unset( $options['strtr'] ); |
| 34 |
} |
| 35 |
|
| 36 |
if ( isset( $options['msr'] ) ) { |
| 37 |
$replacer = new MultiStringReplacer( $zh2Hant ); |
| 38 |
if ( $profile ) { |
| 39 |
xhprof_enable( XHPROF_FLAGS_CPU ); |
| 40 |
} |
| 41 |
$startTime = microtime( true ); |
| 42 |
for ( $i = 0; $i < $loops; $i++ ) { |
| 43 |
$replacer->searchAndReplace( $text ); |
| 44 |
} |
| 45 |
$endTime = microtime( true ); |
| 46 |
$wallTime = 1000 * ( ( $endTime - $startTime ) / $loops ); |
| 47 |
printf( "%-'.40s %.2fms\n", 'MultiStringRepeater::searchAndReplace(): ', $wallTime ); |
| 48 |
if ( $profile ) { |
| 49 |
$profile = xhprof_disable(); |
| 50 |
foreach ( $profile as $func => $data ) { |
| 51 |
printf( "%s: %.2f\n", $func, $data['cpu'] / $data['ct'] ); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
if ( function_exists( 'fss_prep_replace' ) && isset( $options['fss'] ) ) { |
| 57 |
$fss = fss_prep_replace( $zh2Hant ); |
| 58 |
$startTime = microtime( true ); |
| 59 |
for ( $i = 0; $i < $loops; $i++ ) { |
| 60 |
fss_exec_replace( $fss, $text ); |
| 61 |
} |
| 62 |
$endTime = microtime( true ); |
| 63 |
$wallTime = 1000 * ( ( $endTime - $startTime ) / $loops ); |
| 64 |
printf( "%-'.40s %.2fms\n", 'fss_exec_replace(): ', $wallTime ); |
| 65 |
} |
| 66 |
|
| 67 |
if ( isset( $options['strtr'] ) ) { |
| 68 |
$startTime = microtime( true ); |
| 69 |
for ( $i = 0; $i < $loops; $i++ ) { |
| 70 |
strtr( $text, $zh2Hant ); |
| 71 |
} |
| 72 |
$endTime = microtime( true ); |
| 73 |
$wallTime = 1000 * ( ( $endTime - $startTime ) / $loops ); |
| 74 |
printf( "%-'.40s %.2fms\n", 'strtr(): ', $wallTime ); |
| 75 |
} |
| 76 |
|