Advertisement
Tuurlijk

Is First Part Of String Test

Jan 10th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.51 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL & ~E_NOTICE);
  3. /*****************************************************************************
  4.  *  Copyright notice
  5.  *
  6.  *  ⓒ 2013 Michiel Roos <michiel@maxserv.nl>
  7.  *  All rights reserved
  8.  *
  9.  *  This script is part of the TYPO3 project. The TYPO3 project is free
  10.  *  software; you can redistribute it and/or modify it under the terms of the
  11.  *  GNU General Public License as published by the Free Software Foundation;
  12.  *  either version 2 of the License, or (at your option) any later version.
  13.  *
  14.  *  The GNU General Public License can be found at
  15.  *  http://www.gnu.org/copyleft/gpl.html.
  16.  *
  17.  *  This script is distributed in the hope that it will be useful, but
  18.  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  19.  *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  20.  *  more details.
  21.  *
  22.  *  This copyright notice MUST APPEAR in all copies of the script!
  23.  ****************************************************************************/
  24.  
  25. // Setup
  26. $testName = 'isFirstPartOfStr()';
  27. $runs = 5000;
  28.  
  29. // Parameter Sets
  30. $parameterSets = array(
  31.     'set1' => array (
  32.         'description' => 'regular string, not first part',
  33.         'needle' => 'www',
  34.         'haystack' => '/var/www/htdocs/'
  35.     ),
  36.     'set2' => array (
  37.         'description' => 'regular string, first part',
  38.         'needle' => 'www',
  39.         'haystack' => 'www/htdocs/something'
  40.     ),
  41.     'set3' => array (
  42.         'description' => 'empty needle (note that this will of cousre fail v2 and v3)',
  43.         'needle' => '',
  44.         'haystack' => 'www/htdocs/something'
  45.     )
  46. );
  47.  
  48. /*****************************************************************************
  49.  * Tests:
  50.  ****************************************************************************/
  51.  
  52. $descriptions['version1'] = 'Baseline GeneralUtility::isFirstPartOfStr';
  53. function version1($needle, $haystack) {
  54.     return isFirstPartOfStr($haystack, $needle);
  55. }
  56.  
  57. $descriptions['version2'] = 'Using substr(), known needle length';
  58. function version2($needle, $haystack) {
  59.     return substr($haystack, 0, 3) === $needle;
  60. }
  61.  
  62. $descriptions['version3'] = 'Using substr(), known needle length, early return';
  63. function version3($needle, $haystack) {
  64.     return substr($haystack, 0, 3) === $needle;
  65. }
  66.  
  67. $descriptions['version4'] = 'Using substr(), unknown needle length';
  68. function version4($needle, $haystack) {
  69.     return substr($haystack, 0, strlen($needle)) === $needle;
  70. }
  71.  
  72. $descriptions['version5'] = 'Using substr(), unknown needle length, early return';
  73. function version5($needle, $haystack) {
  74.     return substr($haystack, 0, strlen($needle)) === $needle;
  75. }
  76.  
  77. $descriptions['version6'] = 'Using strpos(), known needle length, early return';
  78. function version6($needle, $haystack) {
  79.     return $needle !== '' && strpos($haystack, $needle) === 0;
  80. }
  81.  
  82.  
  83. /*****************************************************************************
  84.  * Helper functions needed for test:
  85.  ****************************************************************************/
  86.  
  87. /**
  88.  * Returns TRUE if the first part of $str matches the string $partStr
  89.  *
  90.  * @param string $str Full string to check
  91.  * @param string $partStr Reference string which must be found as the "first part" of the full string
  92.  * @return boolean TRUE if $partStr was found to be equal to the first part of $str
  93.  */
  94. function isFirstPartOfStr($str, $partStr) {
  95.     return $partStr != '' && strpos((string) $str, (string) $partStr, 0) === 0;
  96. }
  97.  
  98.  
  99.  
  100. /*****************************************************************************
  101.  * System:
  102.  ****************************************************************************/
  103.  
  104. function getmicrotime() {
  105.     $t = gettimeofday();
  106.     return $t['sec'] * 1000 + $t['usec'] / 1000;
  107. }
  108.  
  109. // How many runs?
  110. if (isset($_GET['runs']) && $_GET['runs']) {
  111.     $runs = (preg_replace('/[^0-9]/', '', $_GET['runs'])) ?
  112.         (preg_replace('/[^0-9]/', '', $_GET['runs'])) :
  113.         $runs;
  114. }
  115.  
  116. $baselineTimes = array();
  117. $functions = get_defined_functions();
  118.  
  119. foreach ($parameterSets as $setName => $parameters) {
  120.     $baselineRun = TRUE;
  121.     foreach ($functions['user'] as $function) {
  122.         if (strpos($function, 'version', 0) === 0) {
  123.             $result = array();
  124.             $start = getmicrotime();
  125.             for ($i = 0; $i < $runs; $i++) {
  126.                 $result = $function($parameters['needle'], $parameters['haystack']);
  127.             }
  128.             $end = getmicrotime();
  129.             $time = $end - $start;
  130.             if ($baselineRun) {
  131.                 $baselineTimes[$setName] = $time;
  132.                 $baselineRun = FALSE;
  133.             }
  134.             $xAxis[] = $function;
  135.             if (is_array($result)) {
  136.                 $resultObjects[$setName][$function] = array_slice($result, 0, 20, TRUE);
  137.             } else {
  138.                 $resultObjects[$setName][$function] = $result;
  139.             }
  140.             $times[$setName][$function] = sprintf('%1.3f', $time);
  141.             $i++;
  142.         }
  143.     }
  144. }
  145.  
  146. function findWinner($times) {
  147.     $cumulativeTimes = array();
  148.     foreach ($times as $setName => $timeData) {
  149.         foreach ($timeData as $functionName => $time) {
  150.             if (isset($cumulativeTimes[$functionName])) {
  151.                 $cumulativeTimes['overall'][$functionName] += $time;
  152.                 $cumulativeTimes[$setName][$functionName] += $time;
  153.             } else {
  154.                 $cumulativeTimes['overall'][$functionName] = $time;
  155.                 $cumulativeTimes[$setName][$functionName] = $time;
  156.             }
  157.         }
  158.     }
  159.     $cumulativeTimes = array_filter($cumulativeTimes, 'asort');
  160.     return $cumulativeTimes;
  161. }
  162.  
  163. $cumulativeTimes = (findWinner($times));
  164.  
  165. ?>
  166. <html>
  167. <head>
  168.     <title>TYPO3 Test Suite</title>
  169.     <link rel="stylesheet" href="http://wiki.typo3.org/wiki/load.php?debug=false&amp;lang=en&amp;modules=mediawiki.legacy.commonPrint%2Cshared%7Cskins.typo3vector&amp;only=styles&amp;skin=typo3vector&amp;*" />
  170.     <script src="http://code.jquery.com/jquery-2.0.3.min.js" type="text/javascript"></script>
  171.     <script src="http://code.highcharts.com/highcharts.js" type="text/javascript"></script>
  172.     <script src="http://code.highcharts.com/modules/exporting.js" type="text/javascript"></script>
  173. </head>
  174. <body>
  175. <div id="content" class="mw-body" role="main">
  176.     <h1 id="top"><?php echo $testName ?></h1>
  177.     <p>The overall winning function is <strong><?php echo key(array_slice($cumulativeTimes['overall'], 0, 1)) ?></strong>.</p>
  178.     <?php
  179.         if (count($parameterSets) > 1) {
  180.             echo '<ul>';
  181.             foreach ($cumulativeTimes as $set => $functions) {
  182.                 if ($set === 'overall') { continue; }
  183.                 echo '<li><b>' . $set . '</b>: ' . key(array_slice($functions, 0, 1)) . '</li>';
  184.             }
  185.             echo '</ul>';
  186.         }
  187.     ?>
  188.     <p>Run the tests how many times?</p>
  189.     <form action="<?php echo basename(__FILE__) ?>">
  190.         <input name="runs" id="runs" value="<?php echo $runs ?>"/>
  191.         <input class="submit" type="submit" value="Go!"/>
  192.     </form>
  193.     <div id="resultGraph" style="min-width: 310px; min-height: 400px; margin: 0 auto"></div>
  194.     <?php
  195.         foreach ($times as $setName => $functionData) {
  196.             echo '<h2>' . ucfirst($setName) . '</h2>';
  197.             echo '<p>' . $parameterSets[$setName]['description'] . '</p>';
  198.             echo '<ul>';
  199.             foreach ($parameterSets[$setName] as $key => $value) {
  200.                 if ($key !== 'description') {
  201.                     if (is_array($value)) {
  202.                         echo '<li>' . $key . ':'. '</li>';
  203.                         echo var_dump($value);
  204.                     } else {
  205.                         echo '<li>' . $key . ' = "' . (string) $value . '"</li>';
  206.                     }
  207.                 } else {
  208.                     $setDescriptions[] = $value;
  209.                 }
  210.             }
  211.             echo '</ul>';
  212.             echo '<ul>';
  213.             foreach ($functionData as $function => $time) {
  214.                 $identifier = $setName . '-' . $function;
  215.                 echo '<li><a style="text-decoration: none" href="#' . $identifier . '">' .
  216.                     ucfirst($function) .
  217.                     '</a> ' .
  218.                     ': ' .
  219.                     sprintf('<span style="width: 33px; display: inline-block; text-align: right; font-weight: bold;">%1.2d%%</span> ', $time * 100 / $baselineTimes[$setName]) .
  220.                     $descriptions[$function] .
  221.                     '</li>';
  222.             }
  223.             echo '</ul>';
  224.         }
  225.     ?>
  226.     <script>
  227.         var baseLineTimes = [<?php echo implode(',', $baselineTimes) ?>];
  228.         var descriptions = ['<?php echo implode("','", array_map('addslashes', $descriptions)) ?>'];
  229.         var setDescriptions = ['<?php echo implode("','", array_map('addslashes', $setDescriptions)) ?>'];
  230.         Highcharts.setOptions({
  231.             colors: ['#f58006', '#f50806', '#067bf5', '#f5bc06', '#8006f5']
  232.         });
  233.         jQuery(document).ready(function($) {
  234.             $('#resultGraph').highcharts({
  235.                 chart: {
  236. //                  type: 'bar'
  237.                     zoomType: 'y'
  238.                 },
  239.                 title: {
  240.                     text: '<?php echo $testName ?>'
  241.                 },
  242.                 xAxis: {
  243.                     categories: ['<?php echo implode("','", $xAxis)  ?>'],
  244.                     title: {
  245.                         text: null
  246.                     }
  247.                 },
  248.                 yAxis: {
  249.                     min: 0,
  250.                     title: {
  251.                         text: 'Time (milliseconds)',
  252.                         align: 'high'
  253.                     },
  254.                     labels: {
  255.                         overflow: 'justify',
  256.                         formatter: function() {
  257.                             return Highcharts.numberFormat(this.value, 0, ',', '.');
  258.                         }
  259.                     }
  260.                 },
  261.                 tooltip: {
  262.                     useHTML: true,
  263.                     formatter: function() {
  264.                         return '<strong><a style="text-decoration: none" href="#' + this.point.series.name + '-' + this.point.category + '">' + descriptions[this.point.x] + '</a></strong><br/>' +
  265.                             setDescriptions[this.series.index] + '<br/>' +
  266.                             Highcharts.numberFormat(this.point.y, 0, ',', '.') + ' milliseconds' +
  267.                             ' (' + Math.ceil(this.point.y * 100 / baseLineTimes[this.point.series.index]) + '%)';
  268.                     }
  269.                 },
  270.                 legend: {
  271.                     enabled: true
  272.                 },
  273.                 credits: {
  274.                     enabled: false
  275.                 },
  276.                 series: [
  277.                     <?php
  278.                     foreach ($times as $setName => $setTimes) {
  279.                         $series[] = "{
  280.                             name: '" . $setName . "',
  281.                             data: [" . implode(',', $setTimes) . "]
  282.                         }";
  283.                     }
  284.                     echo implode(',', $series);
  285.                     ?>
  286.                 ]
  287.             });
  288.         });
  289.     </script>
  290.     <h2>Data results</h2>
  291.     <?php
  292.         foreach ($times as $setName => $functionData) {
  293.             echo '<h3>' . ucfirst($setName) . '</h3>';
  294.             echo '<p>' . $parameterSets[$setName]['description'] . '</p>';
  295.             echo '<ul>';
  296.             foreach ($parameterSets[$setName] as $key => $value) {
  297.                 if ($key !== 'description') {
  298.                     echo '<li>' . $key . ' = "' . (string) $value . '"</li>';
  299.                 }
  300.             }
  301.             echo '</ul>';
  302.             foreach ($functionData as $function => $time) {
  303.                 $identifier = $setName . '-' . $function;
  304.                 echo '<h4 id="' . $identifier . '">' . ucfirst($function) . '</h4>';
  305.                 echo '<p>' . $descriptions[$function] . '</p>';
  306.                 var_dump($resultObjects[$setName][$function]);
  307.                 echo '<a href="#top" style="color: #ddd; text-decoration: none;">^ top</a>';
  308.             }
  309.         }
  310.     ?>
  311.     <div id="p-personal" role="navigation" class="">
  312.         <ul>
  313.     <?php
  314.     foreach ($resultObjects as $setName => $functionData) {
  315.         foreach ($functionData as $function => $data) {
  316.         echo '<li><a href="#' . $setName . '-' . $function . '">' . ucfirst($setName) . ' ' . ucfirst($function)  . '</a></h3>';
  317.         }
  318.     }
  319.     ?>
  320.         </ul>
  321.     </div>
  322. </div>
  323. </body>
  324. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement