11 protected $_id = null;
12 protected $_start = null;
18 if($this->_id) $this->times[$this->_id] += microtime(
true) - $this->_start;
27 if(!array_key_exists($this->_id = $id,$this->times)) $this->times[$id] = $this->count[$id] = 0;
28 $this->_start = microtime(
true);
37 if(!array_key_exists($id,$this->times))
return false;
38 $time = $this->times[$id];
39 if($this->_id == $id) $time += microtime(
true) - $this->_start;
47 $total = array_sum($this->times);
48 if($this->_id) $total += microtime(
true) - $this->_start;
52 protected function pad($value,$length){
53 return str_pad($value,$length,
' ',STR_PAD_LEFT);
60 if(!$this->times)
return false;
61 $id_length = max(5,max(array_map(
'strlen',array_keys($this->times))));
62 $time_length = max(8,strlen($total_str = number_format($total_time = $this->total(),$this->decimals)));
63 $count_length = max(5,strlen($count_total_str = number_format(array_sum($this->count))));
66 foreach($this->times as $id => $time) $average_length = max($average_length,strlen($average[$id] = number_format($time / $this->count[$id],min(6,$this->decimals + ceil(log($this->count[$id],10))))));
68 implode(
' | ',[str_pad(
'ID',$id_length),$this->pad(
'time [s]',$time_length),
' %',$this->pad(
'count',$count_length),$this->pad(
'avg [s]',$average_length)]) .
"\n" .
69 ($border = implode(
'-+-',[str_repeat(
'-',$id_length),str_repeat(
'-',$time_length),
'-----',str_repeat(
'-',$count_length),str_repeat(
'-',$average_length)]) .
"\n");
70 foreach($this->times as $id => $time) $text .= implode(
' | ',[
71 str_pad($id,$id_length),
72 $this->pad(number_format($time,$this->decimals),$time_length),
73 $this->pad(number_format(100 * $time / $total_time,1),5),
74 $this->pad(number_format($this->count[$id]),$count_length),
75 $this->pad($average[$id],$average_length)
77 return $text . $border . implode(
' | ',[str_pad(
'Total',$id_length),$this->pad($total_str,$time_length),number_format(100,1),$this->pad($count_total_str,$count_length),
'']);
84 $total_time = $this->total();
85 $html =
"<table class='timer'>\n<tr><th>ID</th><th>time [s]</th><th>%</th><th>count</th><th>avg [s]</th></tr>\n";
86 foreach($this->times as $id => $time) $html .=
'<tr><td>' . implode(
'</td><td>',[
88 number_format($time,$this->decimals),
89 number_format(100 * $time / $total_time,1),
90 number_format($this->count[$id]),
91 number_format($time / $this->count[$id],$this->decimals + ceil(log($this->count[$id],10)))
95 implode(
'</th><th>',[
'Total',number_format($total_time,$this->decimals),number_format(100,1),number_format(array_sum($this->count)),
'']) .
96 "</th></tr>\n</table>";
100 return $this->time($name);
103 public function __call($name,$arguments){
108 return $this->asText();
__call($name, $arguments)
asHtml()
HTML representation of times.
time($id)
Time for a single ID.
asText()
Text representation of times.
stop()
Stop the running timer.