Tuurlijk

substr() vs substr_replace() Test

Jan 15th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.95 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL & ~E_NOTICE);
  3. /*****************************************************************************
  4.  *  Copyright notice
  5.  *
  6.  *  ⓒ 2013 Michiel Roos <[email protected]>
  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 = 'substr() vs substr_replace()';
  27. $runs = 100;
  28.  
  29. // Parameter Sets
  30. $parameterSets = array(
  31.     'set1' => array (
  32.         'string' => 'some.KeyValue',
  33.         'key' => 'some',
  34.         'description' => 'Regular use',
  35.     )
  36. );
  37.  
  38. /*****************************************************************************
  39.  * Tests:
  40.  ****************************************************************************/
  41.  
  42. /**
  43.  * Baseline
  44.  */
  45. $descriptions['version1'] = 'substr()';
  46. function version1($string, $key) {
  47.     return substr($string, strlen($key));
  48. }
  49.  
  50. $descriptions['version2'] = 'substr_replace()';
  51. function version2($string, $key) {
  52.     return substr_replace($string, '', 0, strlen($key));
  53. }
  54.  
  55. /*****************************************************************************
  56.  * Helper functions needed for test:
  57.  ****************************************************************************/
  58.  
  59.  
  60. /*****************************************************************************
  61.  * System:
  62.  ****************************************************************************/
  63.  
  64. function getmicrotime() {
  65.     $t = gettimeofday();
  66.     return $t['sec'] * 1000 + $t['usec'] / 1000;
  67. }
  68.  
  69. // How many runs?
  70. if (isset($_GET['runs']) && $_GET['runs']) {
  71.     $runs = (preg_replace('/[^0-9]/', '', $_GET['runs'])) ?
  72.         (preg_replace('/[^0-9]/', '', $_GET['runs'])) :
  73.         $runs;
  74. }
  75.  
  76. $baselineTimes = array();
  77. $functions = get_defined_functions();
  78.  
  79. foreach ($parameterSets as $setName => $parameters) {
  80.     $baselineRun = TRUE;
  81.     // Description is used later on, so clone the parameters
  82.     $functionParameters = $parameters;
  83.     unset($functionParameters['description']);
  84.     foreach ($functions['user'] as $function) {
  85.         if (strpos($function, 'version', 0) === 0) {
  86.             $result = array();
  87.             $start = getmicrotime();
  88.             for ($i = 0; $i < $runs; $i++) {
  89.                 $functionToCall = new ReflectionFunction($function);
  90.                 $result = $functionToCall->invokeArgs($functionParameters);
  91.             }
  92.             $end = getmicrotime();
  93.             $time = $end - $start;
  94.             if ($baselineRun) {
  95.                 $baselineTimes[$setName] = $time;
  96.                 $baselineRun = FALSE;
  97.             }
  98.             $xAxis[] = $function;
  99.             if (is_array($result)) {
  100.             $resultObjects[$setName][$function] = array_slice($result, 0, 20, TRUE);
  101.             } else {
  102.                 $resultObjects[$setName][$function] = $result;
  103.             }
  104.             $times[$setName][$function] = sprintf('%1.3f', $time);
  105.             $i++;
  106.         }
  107.     }
  108. }
  109.  
  110. function findWinner($times) {
  111.     $cumulativeTimes = array();
  112.     foreach ($times as $setName => $timeData) {
  113.         foreach ($timeData as $functionName => $time) {
  114.             if (isset($cumulativeTimes[$functionName])) {
  115.                 $cumulativeTimes['overall'][$functionName] += $time;
  116.                 $cumulativeTimes[$setName][$functionName] += $time;
  117.             } else {
  118.                 $cumulativeTimes['overall'][$functionName] = $time;
  119.                 $cumulativeTimes[$setName][$functionName] = $time;
  120.             }
  121.         }
  122.     }
  123.     $cumulativeTimes = array_filter($cumulativeTimes, 'asort');
  124.     return $cumulativeTimes;
  125. }
  126.  
  127. $cumulativeTimes = (findWinner($times));
  128.  
  129. ?>
  130. <html>
  131. <head>
  132.     <title><?php  echo $testName ?> | TYPO3 Tiny Test Suite</title>
  133.     <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;*" />
  134.     <script src="http://code.jquery.com/jquery-2.0.3.min.js" type="text/javascript"></script>
  135.     <script src="http://code.highcharts.com/highcharts.js" type="text/javascript"></script>
  136.     <script src="http://code.highcharts.com/modules/exporting.js" type="text/javascript"></script>
  137. </head>
  138. <body>
  139. <div id="content" class="mw-body" role="main">
  140.     <h1 id="top"><?php echo $testName ?></h1>
  141.     <p>The overall winning function is <strong><?php echo key(array_slice($cumulativeTimes['overall'], 0, 1)) ?></strong>.</p>
  142.     <?php
  143.         if (count($parameterSets) > 1) {
  144.             echo '<ul>';
  145.             foreach ($cumulativeTimes as $set => $functions) {
  146.                 if ($set === 'overall') { continue; }
  147.                 echo '<li><b>' . $set . '</b>: ' . key(array_slice($functions, 0, 1)) . '</li>';
  148.             }
  149.             echo '</ul>';
  150.         }
  151.     ?>
  152.     <p>Run the tests how many times?</p>
  153.     <form action="<?php echo basename(__FILE__) ?>">
  154.         <input name="runs" id="runs" value="<?php echo $runs ?>"/>
  155.         <input class="submit" type="submit" value="Go!"/>
  156.     </form>
  157.     <div id="resultGraph" style="min-width: 310px; min-height: 400px; margin: 0 auto"></div>
  158.     <?php
  159.         foreach ($times as $setName => $functionData) {
  160.             echo '<h2>' . ucfirst($setName) . '</h2>';
  161.             echo '<p>' . $parameterSets[$setName]['description'] . '</p>';
  162.             echo '<ul>';
  163.             foreach ($parameterSets[$setName] as $key => $value) {
  164.                 if ($key !== 'description') {
  165.                     if (is_array($value)) {
  166.                         echo '<li>' . $key . ':'. '</li>';
  167.                         echo var_dump($value);
  168.                     } else {
  169.                         echo '<li>' . $key . ' = "' . (string) $value . '"</li>';
  170.                     }
  171.                 } else {
  172.                     $setDescriptions[] = $value;
  173.                 }
  174.             }
  175.             echo '</ul>';
  176.             echo '<ul>';
  177.             foreach ($functionData as $function => $time) {
  178.                 $identifier = $setName . '-' . $function;
  179.                 echo '<li><a style="text-decoration: none" href="#' . $identifier . '">' .
  180.                     ucfirst($function) .
  181.                     '</a> ' .
  182.                     ': ' .
  183.                     sprintf('<span style="width: 33px; display: inline-block; text-align: right; font-weight: bold;">%1.2d%%</span> ', $time * 100 / $baselineTimes[$setName]) .
  184.                     $descriptions[$function] .
  185.                     '</li>';
  186.             }
  187.             echo '</ul>';
  188.         }
  189.     ?>
  190.     <script>
  191.         /**
  192.          * Format an integer as a time value
  193.          *
  194.          * @param {String} time The value to format in microseconds.
  195.          * @param {Number} decimals The amount of decimals
  196.          *
  197.          * @return string
  198.          */
  199.         function printSeconds(time, decimals) {
  200.            decimals = typeof decimals !== 'undefined' ? decimals : 2;
  201.             var prefix = '',
  202.                 suffix = 'μs';
  203.             if (time < 0) {
  204.                 time = Math.abs(time);
  205.                 prefix = '-';
  206.             }
  207.  
  208.             if (time == 0) {
  209.                 suffix = '';
  210.             }
  211.  
  212.             if (time >= 1000) {
  213.                 time = time / 1000;
  214.                 suffix = 'ms';
  215.             }
  216.  
  217.             if (time >= 1000) {
  218.                 time = time / 1000;
  219.                 suffix = ' s';
  220.             }
  221.  
  222.             if (time >= 60 && suffix == ' s') {
  223.                 time = time / 60;
  224.                 suffix = 'min!';
  225.             }
  226.  
  227.             return prefix + Highcharts.numberFormat(time, decimals) + ' ' + suffix;
  228.         }
  229.  
  230.         var baseLineTimes = [<?php echo implode(',', $baselineTimes) ?>];
  231.         var descriptions = ['<?php echo implode("','", array_map('addslashes', $descriptions)) ?>'];
  232.         var setDescriptions = ['<?php echo implode("','", array_map('addslashes', $setDescriptions)) ?>'];
  233.         Highcharts.setOptions({
  234.             colors: ['#f58006', '#f50806', '#067bf5', '#f5bc06', '#8006f5']
  235.         });
  236.         jQuery(document).ready(function($) {
  237.             $('#resultGraph').highcharts({
  238.                 chart: {
  239. //                  type: 'bar'
  240.                     zoomType: 'y'
  241.                 },
  242.                 title: {
  243.                     text: '<?php echo $testName ?>'
  244.                 },
  245.                 xAxis: {
  246.                     categories: ['<?php echo implode("','", $xAxis)  ?>'],
  247.                     title: {
  248.                         text: null
  249.                     }
  250.                 },
  251.                 yAxis: {
  252.                     min: 0,
  253.                     title: {
  254.                         text: 'Time (milliseconds)',
  255.                         align: 'high'
  256.                     },
  257.                     labels: {
  258.                         overflow: 'justify',
  259.                         formatter: function() {
  260.                             return printSeconds(this.value);
  261.                         }
  262.                     }
  263.                 },
  264.                 tooltip: {
  265.                     useHTML: true,
  266.                     formatter: function() {
  267.                         return '<strong><a style="text-decoration: none" href="#' + this.point.series.name + '-' + this.point.category + '">' + descriptions[this.point.x] + '</a></strong><br/>' +
  268.                             setDescriptions[this.series.index] + '<br/>' +
  269.                             printSeconds(this.point.y) +
  270.                             ' (' + Math.ceil(this.point.y * 100 / baseLineTimes[this.point.series.index]) + '%)';
  271.                     }
  272.                 },
  273.                 legend: {
  274.                     enabled: true
  275.                 },
  276.                 credits: {
  277.                     enabled: false
  278.                 },
  279.                 series: [
  280.                     <?php
  281.                     foreach ($times as $setName => $setTimes) {
  282.                         $series[] = "{
  283.                             name: '" . $setName . "',
  284.                             data: [" . implode(',', $setTimes) . "]
  285.                         }";
  286.                     }
  287.                     echo implode(',', $series);
  288.                     ?>
  289.                 ]
  290.             });
  291.         });
  292.     </script>
  293.     <h2>Data results</h2>
  294.     <?php
  295.         foreach ($times as $setName => $functionData) {
  296.             echo '<h3>' . ucfirst($setName) . '</h3>';
  297.             echo '<p>' . $parameterSets[$setName]['description'] . '</p>';
  298.             echo '<ul>';
  299.             foreach ($parameterSets[$setName] as $key => $value) {
  300.                 if ($key !== 'description') {
  301.                     echo '<li>' . $key . ' = "' . (string) $value . '"</li>';
  302.                 }
  303.             }
  304.             echo '</ul>';
  305.             foreach ($functionData as $function => $time) {
  306.                 $identifier = $setName . '-' . $function;
  307.                 echo '<h4 id="' . $identifier . '">' . ucfirst($function) . '</h4>';
  308.                 echo '<p>' . $descriptions[$function] . '</p>';
  309.                 var_dump($resultObjects[$setName][$function]);
  310.                 echo '<a href="#top" style="color: #ddd; text-decoration: none;">^ top</a>';
  311.             }
  312.         }
  313.     ?>
  314.     <div id="p-personal" role="navigation" class="">
  315.         <ul>
  316.     <?php
  317.     foreach ($resultObjects as $setName => $functionData) {
  318.         foreach ($functionData as $function => $data) {
  319.         echo '<li><a href="#' . $setName . '-' . $function . '">' . ucfirst($setName) . ' ' . ucfirst($function)  . '</a></h3>';
  320.         }
  321.     }
  322.     ?>
  323.         </ul>
  324.     </div>
  325. </div>
  326. </body>
  327. </html>
Advertisement
Add Comment
Please, Sign In to add comment