Advertisement
Guest User

Petah

a guest
Dec 9th, 2010
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.00 KB | None | 0 0
  1. <?php
  2. // Initialise input
  3. define('TEST_SIZE', !isset($_GET['test_size']) || !is_numeric($_GET['test_size']) ? 10 : $_GET['test_size']);
  4. define('TYPE', !isset($_GET['type']) || !in_array($_GET['type'], array('array_walk', 'foreach')) ? 'array_walk' : $_GET['type']);
  5. define('TRUE_USAGE', isset($_GET['true_usage']));
  6.  
  7. // Create form for test input
  8. ?>
  9. <form action="" method="get">
  10.     <fieldset>
  11.         <input type="checkbox" name="true_usage" <?php if (TRUE_USAGE) echo 'checked'; ?>>
  12.         <label>True Memory Usage</label>
  13.     </fieldset>
  14.  
  15.     <fieldset>
  16.         <label>Test Size</label>
  17.         <input name="test_size" type="text" value="<?php echo TEST_SIZE; ?>">
  18.     </fieldset>
  19.  
  20.     <fieldset>
  21.         <label>Type</label><br>
  22.         <input name="type" type="radio" value="array_walk" <?php if (TYPE == 'array_walk') echo 'checked'; ?>>
  23.         <label>array_walk</label><br>
  24.         <input name="type" type="radio" value="foreach" <?php if (TYPE == 'foreach') echo 'checked'; ?>>
  25.         <label>foreach</label>
  26.     </fieldset>
  27.  
  28.     <button type="submit">Run</button>
  29. </form>
  30.  
  31. <pre>
  32. Benchmark
  33.  
  34. <?php
  35. // Create a class for testing
  36. class Test {
  37.     private static $memory = null;
  38.     private static $memory_peak = null;
  39.     private static $time = null;
  40.  
  41.     public static function start() {
  42.         self::$memory = memory_get_usage(TRUE_USAGE);
  43.         self::$memory_peak = memory_get_peak_usage(TRUE_USAGE);
  44.         self::$time = microtime(true);
  45.         echo "Test started: ".TYPE.PHP_EOL;
  46.     }
  47.  
  48.     public static function stop() {
  49.         echo "Test stopped".PHP_EOL;
  50.         echo "Memory: \t".number_format(memory_get_usage(TRUE_USAGE) - self::$memory).PHP_EOL;
  51.         echo "Peak memory: \t".number_format(memory_get_peak_usage(TRUE_USAGE) - self::$memory_peak).PHP_EOL;
  52.         echo "Time: \t\t".number_format(microtime(true) - self::$time, 6).PHP_EOL;
  53.     }
  54. }
  55.  
  56. // Initialise the data
  57. $recordset = array();
  58. for ($i = 0; $i++ < TEST_SIZE;) {
  59.     $recordset[] = array('code' => $i, 'quantity' => rand(10, 30), 'amount' => rand(1, 5));
  60. }
  61.  
  62. $total = array('quantity' => 0, 'amount' => 0, 'price' => null);
  63.  
  64. // Run the test
  65. switch (TYPE) {
  66.     case 'array_walk':
  67.         Test::start();
  68.         array_walk($recordset, function (&$value) use(&$total) {
  69.             $value['price'] = ($value['amount'] == 0) ? null : $value['quantity'] / $value['amount'];
  70.             $total['quantity'] += $value['quantity'];
  71.             $total['amount'] += $value['amount'];
  72.         });
  73.         Test::stop();
  74.         break;
  75.     case 'foreach':
  76.         Test::start();
  77.         foreach ($recordset as $value) {
  78.             $value['price'] = ($value['amount'] == 0) ? null : $value['quantity'] / $value['amount'];
  79.             $total['quantity'] += $value['quantity'];
  80.             $total['amount'] += $value['amount'];
  81.         }
  82.         Test::stop();
  83.         break;
  84. }
  85.  
  86. // Get the result
  87. $total['price'] = ($total['amount'] == 0) ? null : $total['quantity'] / $total['amount'];
  88. var_dump($total);
  89. ?>
  90. </pre>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement