Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch '3.0.x' into 3.1.x

  • Loading branch information...
commit 1a63103a0d5b04a7c6cc03f49982dbc6b0d87a61 2 parents 2af332e + c311685
Matt Button BRMatt authored
4 classes/bench/datespan.php
@@ -29,7 +29,7 @@ public function __construct()
29 29 public static function bench_span_original($remote, $local = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds')
30 30 {
31 31 // Array with the output formats
32   - $output = preg_split('/[^a-z]+/', strtolower((string) $output));
  32 + $output = preg_split('/[^a-z]+/', strtolower( (string) $output));
33 33
34 34 // Invalid output
35 35 if (empty($output))
@@ -116,7 +116,7 @@ public static function bench_span_original($remote, $local = NULL, $output = 'ye
116 116 public static function bench_span_use_array($remote, $local = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds')
117 117 {
118 118 // Array with the output formats
119   - $output = preg_split('/[^a-z]+/', strtolower((string) $output));
  119 + $output = preg_split('/[^a-z]+/', strtolower( (string) $output));
120 120
121 121 // Invalid output
122 122 if (empty($output))
2  classes/controller/codebench.php
@@ -17,7 +17,9 @@ public function action_index($class)
17 17 {
18 18 // Convert submitted class name to URI segment
19 19 if (isset($_POST['class']))
  20 + {
20 21 $this->request->redirect('codebench/'.trim($_POST['class']));
  22 + }
21 23
22 24 // Pass the class name on to the view
23 25 $this->template->class = (string) $class;
16 classes/kohana/codebench.php
@@ -155,10 +155,10 @@ public function run()
155 155 foreach ($codebench['benchmarks'] as & $method)
156 156 {
157 157 // Calculate percentage difference relative to fastest and slowest methods
158   - $method['percent']['fastest']['time'] = (empty($fastest_method['time'])) ? 0 : $method['time'] / $fastest_method['time'] * 100;
159   - $method['percent']['fastest']['memory'] = (empty($fastest_method['memory'])) ? 0 : $method['memory'] / $fastest_method['memory'] * 100;
160   - $method['percent']['slowest']['time'] = (empty($slowest_method['time'])) ? 0 : $method['time'] / $slowest_method['time'] * 100;
161   - $method['percent']['slowest']['memory'] = (empty($slowest_method['memory'])) ? 0 : $method['memory'] / $slowest_method['memory'] * 100;
  158 + $method['percent']['fastest']['time'] = (empty($fastest_method['time'])) ? 0 : ($method['time'] / $fastest_method['time'] * 100);
  159 + $method['percent']['fastest']['memory'] = (empty($fastest_method['memory'])) ? 0 : ($method['memory'] / $fastest_method['memory'] * 100);
  160 + $method['percent']['slowest']['time'] = (empty($slowest_method['time'])) ? 0 : ($method['time'] / $slowest_method['time'] * 100);
  161 + $method['percent']['slowest']['memory'] = (empty($slowest_method['memory'])) ? 0 : ($method['memory'] / $slowest_method['memory'] * 100);
162 162
163 163 // Assign a grade for time and memory to each method
164 164 $method['grade']['time'] = $this->_grade($method['percent']['fastest']['time']);
@@ -168,10 +168,10 @@ public function run()
168 168 foreach ($method['subjects'] as & $subject)
169 169 {
170 170 // Calculate percentage difference relative to fastest and slowest subjects for this method
171   - $subject['percent']['fastest']['time'] = (empty($fastest_subject['time'])) ? 0 : $subject['time'] / $fastest_subject['time'] * 100;
172   - $subject['percent']['fastest']['memory'] = (empty($fastest_subject['memory'])) ? 0 : $subject['memory'] / $fastest_subject['memory'] * 100;
173   - $subject['percent']['slowest']['time'] = (empty($slowest_subject['time'])) ? 0 : $subject['time'] / $slowest_subject['time'] * 100;
174   - $subject['percent']['slowest']['memory'] = (empty($slowest_subject['memory'])) ? 0 : $subject['memory'] / $slowest_subject['memory'] * 100;
  171 + $subject['percent']['fastest']['time'] = (empty($fastest_subject['time'])) ? 0 : ($subject['time'] / $fastest_subject['time'] * 100);
  172 + $subject['percent']['fastest']['memory'] = (empty($fastest_subject['memory'])) ? 0 : ($subject['memory'] / $fastest_subject['memory'] * 100);
  173 + $subject['percent']['slowest']['time'] = (empty($slowest_subject['time'])) ? 0 : ($subject['time'] / $slowest_subject['time'] * 100);
  174 + $subject['percent']['slowest']['memory'] = (empty($slowest_subject['memory'])) ? 0 : ($subject['memory'] / $slowest_subject['memory'] * 100);
175 175
176 176 // Assign a grade letter for time and memory to each subject
177 177 $subject['grade']['time'] = $this->_grade($subject['percent']['fastest']['time']);
23 config/userguide.php
... ... @@ -0,0 +1,23 @@
  1 +<?php defined('SYSPATH') or die('No direct script access.');
  2 +
  3 +return array(
  4 + // Leave this alone
  5 + 'modules' => array(
  6 +
  7 + // This should be the path to this modules userguide pages, without the 'guide/'. Ex: '/guide/modulename/' would be 'modulename'
  8 + 'codebench' => array(
  9 +
  10 + // Whether this modules userguide pages should be shown
  11 + 'enabled' => TRUE,
  12 +
  13 + // The name that should show up on the userguide index page
  14 + 'name' => 'Codebench',
  15 +
  16 + // A short description of this module, shown on the index page
  17 + 'description' => 'Code benchmarking tool.',
  18 +
  19 + // Copyright message, shown in the footer for this module
  20 + 'copyright' => '&copy; 2008–2010 Kohana Team',
  21 + )
  22 + )
  23 +);
76 guide/codebench/index.md
Source Rendered
... ... @@ -0,0 +1,76 @@
  1 +# Using Codebench
  2 +
  3 +[!!] The contents of this page are taken (with some minor changes) from <http://www.geertdedeckere.be/article/introducing-codebench> and are copyright Geert De Deckere.
  4 +
  5 +For a long time I have been using a quick-and-dirty `benchmark.php` file to optimize bits of PHP code, many times regex-related stuff. The file contained not much more than a [gettimeofday](http://php.net/gettimeofday) function wrapped around a `for` loop. It worked, albeit not very efficiently. Something more solid was needed. I set out to create a far more usable piece of software to aid in the everlasting quest to squeeze every millisecond out of those regular expressions.
  6 +
  7 +## Codebench Goals
  8 +
  9 +### Benchmark multiple regular expressions at once
  10 +
  11 +Being able to compare the speed of an arbitrary amount of regular expressions would be tremendously useful. In case you are wondering—yes, I had been writing down benchmark times for each regex, uncommenting them one by one. You get the idea. Those days should be gone forever now.
  12 +
  13 +### Benchmark multiple subjects at once
  14 +
  15 +What gets overlooked too often when testing and optimizing regular expressions is the fact that speed can vastly differ depending on the subjects, also known as input or target strings. Just because your regular expression matches, say, a valid email address quickly, does not necessarily mean it will quickly realize when an invalid email is provided. I plan to write a follow-up article with hands-on regex examples to demonstrate this point. Anyway, Codebench allows you to create an array of subjects which will be passed to each benchmark.
  16 +
  17 +### Make it flexible enough to work for all PCRE functions
  18 +
  19 +Initially I named the module “Regexbench”. I quickly realized, though, it would be flexible enough to benchmark all kinds of PHP code, hence the change to “Codebench”. While tools specifically built to help profiling PCRE functions, like [preg_match](http://php.net/preg_match) or [preg_replace](http://php.net/preg_replace), definitely have their use, more flexibility was needed here. You should be able to compare all kinds of constructions like combinations of PCRE functions and native PHP string functions.
  20 +
  21 +### Create clean and portable benchmark cases
  22 +
  23 +Throwing valuable benchmark data away every time I needed to optimize another regular expression had to stop. A clean file containing the complete set of all regex variations to compare, together with the set of subjects to test them against, would be more than welcome. Moreover, it would be easy to exchange benchmark cases with others.
  24 +
  25 +### Visualize the benchmarks
  26 +
  27 +Obviously providing a visual representation of the benchmark results, via simple graphs, would make interpreting them easier. Having not to think about Internet Explorer for once, made writing CSS a whole lot more easy and fun. It resulted in some fine graphs which are fully resizable.
  28 +
  29 +Below are two screenshots of Codebench in action. `Valid_Color` is a class made for benchmarking different ways to validate hexadecimal HTML color values, e.g. `#FFF`. If you are interested in the story behind the actual regular expressions, take a look at [this topic in the Kohana forums](http://forum.kohanaphp.com/comments.php?DiscussionID=2192).
  30 +
  31 +![Benchmarking several ways to validate HTML color values](codebench_screenshot1.png)
  32 +**Benchmarking seven ways to validate HTML color values**
  33 +
  34 +![Collapsable results per subject for each method](codebench_screenshot2.png)
  35 +**Collapsable results per subject for each method**
  36 +
  37 +## Working with Codebench
  38 +
  39 +Codebench is included in Kohana 3, but if you need you [can download it](http://github.com/kohana/codebench/) from GitHub. Be sure Codebench is activated in your `application/bootstrap.php`.
  40 +
  41 +Creating your own benchmarks is just a matter of creating a class that extends the Codebench class. The class should go in `classes/bench` and the class name should have the `Bench_` prefix. Put the code parts you want to compare into separate methods. Be sure to prefix those methods with `bench_`, other methods will not be benchmarked. Glance at the files in `modules/codebench/classes/bench/` for more examples.
  42 +
  43 +Here is another short example with some extra explanations.
  44 +
  45 + // classes/bench/ltrimdigits.php
  46 + class Bench_LtrimDigits extends Codebench {
  47 +
  48 + // Some optional explanatory comments about the benchmark file.
  49 + // HTML allowed. URLs will be converted to links automatically.
  50 + public $description = 'Chopping off leading digits: regex vs ltrim.';
  51 +
  52 + // How many times to execute each method per subject.
  53 + // Total loops = loops * number of methods * number of subjects
  54 + public $loops = 100000;
  55 +
  56 + // The subjects to supply iteratively to your benchmark methods.
  57 + public $subjects = array
  58 + (
  59 + '123digits',
  60 + 'no-digits',
  61 + );
  62 +
  63 + public function bench_regex($subject)
  64 + {
  65 + return preg_replace('/^\d+/', '', $subject);
  66 + }
  67 +
  68 + public function bench_ltrim($subject)
  69 + {
  70 + return ltrim($subject, '0..9');
  71 + }
  72 + }
  73 +
  74 +
  75 +
  76 +And the winner is… [ltrim](http://php.net/ltrim). Happy benchmarking!
1  guide/codebench/menu.md
Source Rendered
... ... @@ -0,0 +1 @@
  1 +## [Codebench]()
BIN  media/guide/codebench/codebench_screenshot1.png
BIN  media/guide/codebench/codebench_screenshot2.png
4 views/codebench.php
@@ -14,7 +14,9 @@
14 14 <head>
15 15
16 16 <meta charset="utf-8" />
17   - <title><?php if ($class !== '') echo $class, ' · ' ?>Codebench</title>
  17 + <title><?php if ($class !== ''): ?>
  18 + <?php echo $class, ' · ' ?>
  19 + <?php endif; ?>Codebench</title>
18 20
19 21 <style>
20 22 /* General styles*/

0 comments on commit 1a63103

Please sign in to comment.
Something went wrong with that request. Please try again.