Unicode normalization routines.
[lhc/web/wiklou.git] / includes / normal / UtfNormalBench.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 require_once 'UtfNormalUtil.php';
21 require_once 'UtfNormal.php';
22
23 if( php_sapi_name() != 'cli' ) {
24 die( "Run me from the command line please.\n" );
25 }
26
27 $testfiles = array(
28 'testdata/washington.txt' => 'English text',
29 'testdata/berlin.txt' => 'German text',
30 'testdata/tokyo.txt' => 'Japanese text',
31 'testdata/byzantium.txt' => 'Korean text'
32 );
33 $normalizer = new UtfNormal;
34 foreach( $testfiles as $file => $desc ) {
35 benchmarkTest( $normalizer, $file, $desc );
36 }
37
38 # -------
39
40 function benchmarkTest( &$u, $filename, $desc ) {
41 print "Testing $filename ($desc)...\n";
42 $data = file_get_contents( $filename );
43 $forms = array( 'placebo',
44 'fastDecompose', 'fastCombiningSort', 'fastCompose',
45 'toNFD', 'toNFKD', 'toNFC', 'toNFKC',
46 'NFD', 'NFKD', 'NFC', 'NFKC' );
47 foreach( $forms as $form ) {
48 benchmarkForm( $u, $data, $form );
49 }
50 }
51
52 function benchTime(){
53 $st = explode( ' ', microtime() );
54 return (float)$st[0] + (float)$st[1];
55 }
56
57 function benchmarkForm( &$u, &$data, $form ) {
58 global $utfCanonicalDecomp;
59 $start = benchTime();
60 $out = $u->$form( $data, $utfCanonicalDecomp );
61 $delta = benchTime() - $start;
62 $same = (0 == strcmp( $data, $out ) );
63
64 printf( " %4s %1.4fs (%s)\n", $form, $delta, ($same ? 'no change' : 'changed' ) );
65 }
66
67 ?>