Extension:StringFunctions/Bench
From MediaWiki.org
NOTE: This is NOT the extension, this is a broken TESTING CODE.
<?php /* {{#len:value}} Returns the length of the given value. See: http://php.net/manual/function.strlen.php =========================================================== Copyright (c) 2008 Ross McClure & Juraj Simlovic http://www.mediawiki.org/wiki/User:Algorithm http://www.mediawiki.org/wiki/User:jsimlo Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ $wgExtensionCredits['parserhook'][] = array( 'name' => 'StringFunctionsTest', 'description' => 'Benchmarks various implementations of StringFunctions', 'author' => array('Ross McClure', 'Juraj Simlovic'), 'url' => 'http://www.mediawiki.org/wiki/Extension:StringFunctions', ); $wgExtensionFunctions[] = 'wfStringFunctions'; $wgHooks['LanguageGetMagic'][] = 'wfStringFunctionsLanguageGetMagic'; function wfStringFunctions ( ) { global $wgParser, $wgExtStringFunctions; global $wgStringFunctionsLimitSearch; global $wgStringFunctionsLimitReplace; global $wgStringFunctionsLimitPad; $wgExtStringFunctions = new ExtStringFunctions ( ); $wgStringFunctionsLimitSearch = 30; $wgStringFunctionsLimitReplace = 30; $wgStringFunctionsLimitPad = 100; $wgParser->setFunctionHook('len', array(&$wgExtStringFunctions,'runLen' )); $wgParser->setFunctionHook('pos', array(&$wgExtStringFunctions,'runPos' )); $wgParser->setFunctionHook('rpos', array(&$wgExtStringFunctions,'runRPos' )); $wgParser->setFunctionHook('sub', array(&$wgExtStringFunctions,'runSub' )); $wgParser->setFunctionHook('pad', array(&$wgExtStringFunctions,'runPad' )); $wgParser->setFunctionHook('replace', array(&$wgExtStringFunctions,'runReplace' )); $wgParser->setFunctionHook('explode', array(&$wgExtStringFunctions,'runExplode' )); $wgParser->setFunctionHook('urlencode',array(&$wgExtStringFunctions,'runUrlEncode')); $wgParser->setFunctionHook('urldecode',array(&$wgExtStringFunctions,'runUrlDecode')); } function wfStringFunctionsLanguageGetMagic( &$magicWords, $langCode = "en" ) { switch ( $langCode ) { default: $magicWords['len'] = array ( 0, 'len' ); $magicWords['pos'] = array ( 0, 'pos' ); $magicWords['rpos'] = array ( 0, 'rpos' ); $magicWords['sub'] = array ( 0, 'sub' ); $magicWords['pad'] = array ( 0, 'pad' ); $magicWords['replace'] = array ( 0, 'replace' ); $magicWords['explode'] = array ( 0, 'explode' ); $magicWords['urlencode'] = array ( 0, 'urlencode' ); $magicWords['urldecode'] = array ( 0, 'urldecode' ); } return true; } class ExtStringFunctions { /** * Retreives marker prefix. */ function mwPrefix ( &$parser ) { return $parser->mUniqPrefix; } /** * Retreives marker suffix. */ function mwSuffix ( &$parser ) { if( isset($parser->mMarkerSuffix) ) return $parser->mMarkerSuffix; if ( strcmp( MW_PARSER_VERSION, '1.6.1' ) > 0 ) return "QINU\x07"; return'QINU'; } ############################################################################ /** * Simplest implementation, which is however not working correctly. * The calls to mwPrefix() and mwSuffix() are kept here to ensure the * same overhead for requisite data retreival. The values are added to * the total to ensure that the zend is not going to optimize 'em out. */ function runLen0 ( &$parser, $inStr = '' ) { $prefix = $this->mwPrefix ($parser); $suffix = $this->mwSuffix ($parser); return mb_strlen ( (string)$inStr ) + $prefix + $suffix; } /** * Former implementation, which divides the string into an array of * multibyte characters and counts them up. Downfall of this simple * preg implementation is that the preg_match_all allocates/exhausts * an array of all matches (i.e. array of all individual characters). */ function runLen1 ( &$parser, $inStr = '' ) { $prefix = $this->mwPrefix ($parser); $suffix = $this->mwSuffix ($parser); return preg_match_all ( '/' . preg_quote( $prefix, '/' ) . '.*?' . preg_quote( $suffix, '/' ) . '|./su', $inStr, $arr ); } /** * This implementation is based on the previous one, but instead * of counting individual multibyte characters, it counts lengths * of markers and substract them from the total mbstring length. */ function runLen2 ( &$parser, $inStr = '' ) { $prefix = $this->mwPrefix ($parser); $suffix = $this->mwSuffix ($parser); $count = preg_match_all ( '/' . preg_quote( $prefix, '/' ) . '.*?' . preg_quote( $suffix, '/' ) . '/', (string) $inStr, $matches ); $len = mb_strlen ( (string) $inStr ); foreach ($matches[0] as $match) $len -= strlen ($match) - 1; return $len; } /** * This implementation is based on the previous one, but replaces * the call to preg_match_all witch a loop of preg_match calls. */ function runLen3 ( &$parser, $inStr = '' ) { $prefix = $this->mwPrefix ($parser); $suffix = $this->mwSuffix ($parser); $pattern = '/' . preg_quote( $prefix, '/' ) . '.*?' . preg_quote( $suffix, '/' ) . '/'; $len = mb_strlen ( (string) $inStr ); while (preg_match ($pattern, (string) $inStr, $matches, PREG_OFFSET_CAPTURE, $offset)) { $len -= ($l = strlen ($matches[0][0])) - 1; $offset = $matches[0][1] + $l; } return $len; } /** * This implementation searches for markers the old non-preg way. * Counts lengths of individual markers and hen substract them from * the total mbstring length. */ function runLen4 ( &$parser, $inStr = '' ) { $prefix = $this->mwPrefix ($parser); $suffix = $this->mwSuffix ($parser); $add = strlen ($suffix) - 1; $len = 0; $offset = 0; while (false !== ($pos = strpos ($inStr, $prefix, $offset))) { $offset = strpos ($inStr, $suffix, $pos) + $add; $len += $offset - $pos; } return mb_strlen ($inStr) - $len; } ############################################################################ /** * Attempts to bench {{#len:value}} implementations. */ function runLen ( &$parser, $inStr = '' ) { // first verify that all the implementations are qualified $i = $this->runLen1 ( $parser, $inStr ); $i === $this->runLen2 ( $parser, $inStr ) or die ("2 not equal"); $i === $this->runLen3 ( $parser, $inStr ) or die ("3 not equal"); $i === $this->runLen4 ( $parser, $inStr ) or die ("4 not equal"); // setup number of loops and the length multilier $loops = 256; $multi = 10240; // create testing data by repeating the testing string.. $data = str_repeat ( $inStr, $multi ); echo 'benching '.$loops.' loops of length: ' . (strlen ($data) / 1024) . "kB<br>"; flush (); // bench each implementation $b = microtime (true); for ($i = $loops; $i-- > 0; ) $this->runLen0 ( $parser, $data ); echo "runLen0: ". sprintf ("%.4f<br>", microtime (true) - $b ); flush (); // $b = microtime (true); // for ($i = $loops; $i-- > 0; ) $this->runLen1 ( $parser, $data ); // echo "runLen1: ". sprintf ("%.4f<br>", microtime (true) - $b ); // flush (); $b = microtime (true); for ($i = $loops; $i-- > 0; ) $this->runLen2 ( $parser, $data ); echo "runLen2: ". sprintf ("%.4f<br>", microtime (true) - $b ); flush (); // $b = microtime (true); // for ($i = $loops; $i-- > 0; ) $this->runLen3 ( $parser, $data ); // echo "runLen3: ". sprintf ("%.4f<br>", microtime (true) - $b ); // flush (); $b = microtime (true); for ($i = $loops; $i-- > 0; ) $this->runLen4 ( $parser, $data ); echo "runLen4: ". sprintf ("%.4f<br>", microtime (true) - $b ); flush (); die ('Benchmark completed!'); } }
