0x00

[PHP] Benchmarking

Jul 12th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.48 KB | None | 0 0
  1. <?php
  2.     /*
  3.         * @class benchmarking tools
  4.         * @author Jordan Doyle
  5.         * @description use this for simple benchmarking!
  6.     */
  7.     namespace benchmark;
  8.     class tools
  9.     {
  10.         /*
  11.             * @function **none**
  12.             * @description defines vars
  13.         */
  14.             public $_start,
  15.                    $_end;
  16.                    $_benchmark;
  17.  
  18.  
  19.         /*
  20.             * @function start
  21.             * @description starts the timer. (microtime(true))
  22.         */
  23.         public function start()
  24.         {
  25.             $this->_start = microtime(true);
  26.             return true;
  27.         }
  28.         /*
  29.             * @function stop
  30.             * @description stops the timer. (microtime(true)).
  31.         */
  32.         public function stop()
  33.         {
  34.             $this->_end = microtime(true);
  35.             return $this;
  36.         }
  37.         /*
  38.             * @function end
  39.             * @description finishes everything by subtracting the 2 floats and working out the decimals.
  40.         */
  41.         public function end($decimals)
  42.         {
  43.             $this->_benchmark = numberformat( ($this->_start - $this->_end), $decimals );
  44.             return $this->_benchmark;
  45.         }
  46.         /*
  47.             * @function flush
  48.             * @description nulls all the vars.
  49.         */
  50.         public function flush()
  51.         {
  52.             $this->_benchmark = NULL;
  53.             $this->_start     = NULL;
  54.             $this->_end       = NULL;
  55.             return $this;
  56.         }
  57.     }
  58.    
  59.     // make an instance of the class, using your singleton (\benchmark\tools) name the var 'b'
  60.     // now you need to start the benchmark, $b->start();
  61.     // now when you need to stop it use $b->stop();
  62.     // to output it... echo $b->end(5);
  63.     // -----OR WITH THE NEW UPDATE YOU CAN NOW DO $b->stop()->end(5); The integer in end is how many decimals...
Advertisement
Add Comment
Please, Sign In to add comment