Benchmark Klasse

  • Hallo zusammen,


    ich habe mal eine kleine Benchmark Klasse geschrieben und würde gerne wissen was ihr davon haltet.


    benchmark.php

    PHP
    1. <span class="syntaxhtml"><br /><span class="syntaxdefault"><?php<br /><br />   </span><span class="syntaxcomment">/**<br />    * A simple benchmark class to meassure execution times<br />    *<br />    * @author Starhunter<br />    *<br />    * @version 0.9.0.0<br />    */<br /></span><span class="syntaxdefault">     <br />   class Benchmark<br />   </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">      </span><span class="syntaxcomment">/**<br />       *<br />       * @var array<br />       *<br />       * @access private<br />       */<br /></span><span class="syntaxdefault">       protected $times </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> array</span><span class="syntaxkeyword">();<br /><br /></span><span class="syntaxdefault">       function __construct</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$name </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">""</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">       </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">          $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">name </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $name</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">       </span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">       function start</span><span class="syntaxkeyword">()<br /></span><span class="syntaxdefault">       </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">          $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">times</span><span class="syntaxkeyword">[</span><span class="syntaxstring">"Start"</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> microtime</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">true</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">       </span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">       function getName</span><span class="syntaxkeyword">()<br /></span><span class="syntaxdefault">       </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">          return $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">name</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">       </span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">       function setMarker</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$marker_name</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">       </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">         $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">times</span><span class="syntaxkeyword">[</span><span class="syntaxstring">"$marker_name"</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> microtime</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">true</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">       </span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">       function stop</span><span class="syntaxkeyword">()<br /></span><span class="syntaxdefault">       </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">          $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">times</span><span class="syntaxkeyword">[</span><span class="syntaxstring">"Stop"</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> microtime</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">true</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">       </span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">       function getResults</span><span class="syntaxkeyword">()<br /></span><span class="syntaxdefault">       </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">         return $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">times</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">       </span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">       function getTime</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$start</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $end</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">       </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">          if </span><span class="syntaxkeyword">(!isset(</span><span class="syntaxdefault">$this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">times</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">$start</span><span class="syntaxkeyword">]))<br /></span><span class="syntaxdefault">          </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">             throw new Exception</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Marker $start is not defined"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">          </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">          if </span><span class="syntaxkeyword">(!isset(</span><span class="syntaxdefault">$this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">times</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">$end</span><span class="syntaxkeyword">]))<br /></span><span class="syntaxdefault">          </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">             throw new Exception</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Marker $end is not defined"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">          </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">          <br />          return $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">times</span><span class="syntaxkeyword">[</span><span class="syntaxstring">"$end"</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">-</span><span class="syntaxdefault"> $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">times</span><span class="syntaxkeyword">[</span><span class="syntaxstring">"$start"</span><span class="syntaxkeyword">];<br /></span><span class="syntaxdefault">          <br />       </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">   </span><span class="syntaxkeyword">}</span><span class="syntaxdefault"> </span><span class="syntaxcomment">// end of class Bechmark<br /></span><span class="syntaxdefault"> <br />?><br /></span></span>


    index.php

    PHP
    1. <span class="syntaxhtml"><br /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><br /><html><br />    <head><br />        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br />        <title></title><br />    </head><br />    <body><br />        <span class="syntaxdefault"><?php<br />         require_once </span><span class="syntaxstring">'benchmark.php'</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">         function for_loop</span><span class="syntaxkeyword">()<br /></span><span class="syntaxdefault">         </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">            for </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$i</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> $i </span><span class="syntaxkeyword"><</span><span class="syntaxdefault"> 10000</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> $i</span><span class="syntaxkeyword">++)<br /></span><span class="syntaxdefault">            </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">               $j </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $i </span><span class="syntaxkeyword">*</span><span class="syntaxdefault"> 1.2345678 </span><span class="syntaxkeyword">/</span><span class="syntaxdefault"> 12345</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">            </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">         </span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">         function while_loop</span><span class="syntaxkeyword">()<br /></span><span class="syntaxdefault">         </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">            $i </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> 0</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">            while </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$i </span><span class="syntaxkeyword"><</span><span class="syntaxdefault"> 10000</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">            </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">              $j </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $i </span><span class="syntaxkeyword">*</span><span class="syntaxdefault"> 1.2345678 </span><span class="syntaxkeyword">/</span><span class="syntaxdefault"> 12345</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">              $i</span><span class="syntaxkeyword">++;<br /></span><span class="syntaxdefault">            </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">         </span><span class="syntaxkeyword">}<br /><br /><br /></span><span class="syntaxdefault">         $BM </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> new Benchmark</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Demo"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">            <br />            $BM</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">start</span><span class="syntaxkeyword">();<br /><br /></span><span class="syntaxdefault">            </span><span class="syntaxcomment">// Test the for loop<br /><br /></span><span class="syntaxdefault">         $BM</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">setMarker</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"for_start"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">            <br />            for_loop</span><span class="syntaxkeyword">();<br /></span><span class="syntaxdefault">            <br />            $BM</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">setMarker</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"for_end"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">            <br />            </span><span class="syntaxcomment">// Test the while loop<br /></span><span class="syntaxdefault">            <br />            $BM</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">setMarker</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"while_start"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">            <br />            while_loop</span><span class="syntaxkeyword">();<br /></span><span class="syntaxdefault">            <br />            $BM</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">setMarker</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"while_end"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">            <br />            $BM</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">stop</span><span class="syntaxkeyword">();<br /></span><span class="syntaxdefault">            <br />            </span><span class="syntaxcomment">// Get the results<br /></span><span class="syntaxdefault">            <br />            $for_time </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $BM</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">getTime</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"for_start"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">"for_end"</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">            <br />            $while_time </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $BM</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">getTime</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"while_start"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">"while_end"</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">            <br />            $total_time </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $BM</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">getTime</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Start"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">"Stop"</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">            <br />            </span><span class="syntaxcomment">// Display the results now<br /></span><span class="syntaxdefault">            <br />            println</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Results: "</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">            hr</span><span class="syntaxkeyword">();<br /></span><span class="syntaxdefault">            println</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Running time for loop: "</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> number_format</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$for_time</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">" seconds"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">            hr</span><span class="syntaxkeyword">();<br /></span><span class="syntaxdefault">            println</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Running time while loop: "</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> number_format</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$while_time</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">" seconds"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">            hr</span><span class="syntaxkeyword">();<br /></span><span class="syntaxdefault">            println</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Total running time: "</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> number_format</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$total_time</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">" seconds"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">            <br />            <br />            </span><span class="syntaxcomment">// Simple helper function<br /></span><span class="syntaxdefault">            <br />            function hr</span><span class="syntaxkeyword">()<br /></span><span class="syntaxdefault">            </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">                print</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"<hr>"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">            </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">            <br />            function println</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$msg </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">""</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">            </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">                print</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"\n<br>$msg"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">            </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">            <br />        ?><br /></span>    </body><br /></html><br /><br /></span>


    MfG