test_seconds; $count = 0; $start = microtime(1); $time = 0; while ($time < $seconds) { $ret = call_user_func_array($func,$args); $time = microtime(1) - $start; $count++; } $this->results['count'][$name] = intval($count / $seconds); $this->results['return'][$name] = $ret; } public function summary() { if (php_sapi_name() === 'cli') { $this->text_summary(); } else { $this->html_summary(); } } public function text_summary() { if (sizeof($this->results['count']) === 0) { print "No results found"; return false; } $php_version = phpversion(); print "PHP Version: $php_version\n\n"; arsort($this->results['count']); $test_names = array_keys($this->results['count']); $max_len = 0; $total_len = 0; foreach ($test_names as $name) { $total_len += strlen($name); if (strlen($name) > $max_len) { $max_len = strlen($name); } } ############################################## print str_repeat(" ",$max_len + 4); print join(" | ",$test_names) . "\n"; // The length of the bar is the total length of the test names, // plus the " | " between each word test pair, // plus four because there are two spaces at the start and end $additional = (sizeof($test_names) - 1) * 3; $bar = str_repeat(" ",$max_len + 2) . str_repeat("-",$total_len + $additional + 4) . "\n"; print $bar; foreach($test_names as $y_name) { printf(" %{$max_len}s |",$y_name); foreach ($test_names as $x_name) { $x_val = $this->results['count'][$x_name]; $y_val = $this->results['count'][$y_name]; $percent = round(($y_val / $x_val) * 100,2) . "%"; $col_width = strlen($x_name) + 1; if ($y_val == $x_val) { $percent = "N/A"; } //printf(" %{$col_width}s",$percent); printf("%{$col_width}s |","$percent"); } print "\n"; } print $bar; print "\n"; $max_len += 1; $expected_results = reset($this->results['return']); //print_r($this->results['count']); foreach($test_names as $name) { $count = $this->results['count'][$name]; $ret = $this->results['return'][$name]; if ($this->include_sample_output) { printf(" %s = %d interations per second\n",$name,$count); if ($this->show_differences && ($ret !== $expected_results)) { print " ** Return value from this function differs from the first test **\n"; } $print_r_output = trim(print_r($ret,true)); $print_r_output = preg_replace("/^/m"," ",$print_r_output); print " Sample output: \n$print_r_output\n\n"; } else { printf("%{$max_len}s = %d interations per second\n",$name,$count); } } } public function html_summary() { if (sizeof($this->results['count']) === 0) { print "
No results found
"; return false; } arsort($this->results['count']); $tests = array_keys($this->results['count']); array_unshift($tests,' '); $first = 1; $php_version = phpversion(); $out = "

PHP Version: $php_version

\n"; $header_color = '#CCE6FF'; $out .= "\n"; $x = 0; foreach ($tests as $name) { $out .= "\t\n"; if ($first) { foreach ($tests as $i) { $out .= "\t\t\n"; } $first = 0; } else { $column = 0; $y = 0; foreach ($tests as $i) { if ($column == 0) { $content = $tests[$x + 1]; $align = 'right'; $x++; $color = $header_color; $fw = 'bold'; } else { $y++; $x_name = $tests[$x]; $y_name = $tests[$y]; $a = $this->results['count'][$x_name]; $b = $this->results['count'][$y_name]; $percentage = sprintf("%.2f%%",($a / $b) * 100); if ($x === $y) { $content = '
N/A
'; } else { $content = "$percentage"; } $align = 'center'; $color = 'white'; $fw = 'normal'; } $out .= "\t\t\n"; $column++; } } $out .= "\t\n"; } $out .= "
$i$content
"; $out .= "
"; $slowest = min($this->results['count']); $fastest = max($this->results['count']); $expected_results = reset($this->results['return']); foreach ($this->results['count'] as $test => $speed) { if ($speed == $fastest) { $extra = "(Fastest)"; } elseif ($speed == $slowest) { $extra = "(Slowest)"; } else { $extra = ''; } $ret = $this->results['return'][$test]; $out .= "
$test = $speed iterations per second $extra
"; if ($this->show_differences && ($ret !== $expected_results)) { $out .= "
Note: Return value from this function differs from the first test
\n"; } if ($this->include_sample_output) { $out .= "
Sample output: " . print_r($ret,true) . "
\n"; } } print $out; } public function reset() { $this->results = array(); } } // End of class ////////////////////////////////////////////////////////////////////////////// $str = " Monday Tuesday Wednesday Thursday Friday\nSaturday\tSunday"; $a = function($str) { return str_word_count($str,1,"!\"#$%&'()*+,./0123456789-:;<=>?@[\]^_`{|}~"); }; $c = function($str) { return preg_split("/\s+/",trim($str)); }; $d = function($string){ return array_filter(explode(' ', implode(' ', array_map('trim', explode("\n", $string))))); }; $e = function($str) { $str = trim($str); $words = array(); $len = strlen($str); $word = ''; for ($i = 0; $i < $len; $i++) { $char = $str[$i]; $ord = ord($char); // Is a character if ($ord > 32 && $ord < 127) { $word .= $char; // It's some sort of whitespace } else { if ($word) { $words[] = $word; } $word = ''; } } if ($word) { $words[] = $word; } return $words; }; $f = function($str,$return_hash = false) { $str = trim($str); // Word characters are any printable char $words = str_word_count($str,1,"!\"#$%&'()*+,./0123456789-:;<=>?@[\]^_`{|}~"); if ($return_hash) { $ret = array(); $num = sizeof($words); // Odd number of elements, can't build a hash if ($num % 2 == 1) { return array(); } else { // Loop over each word and build a key/value hash for ($i = 0; $i < $num; $i += 2) { $key = $words[$i]; $value = $words[$i + 1]; $ret[$key] = $value; } return $ret; } } else { return $words; } }; ////////////(////////////////////////////////////////////////////////////////// $bm = new benchmark; $bm->include_sample_output = 0; // Include sample return data in HTML summary $bm->show_differences = 1; // Show differences in the function return $bm->test_seconds = 1; // Number of seconds to run each test for // Run the benchmark on each function // $bm->time_this($name, $anonymous_function, array of arguments to pass); $bm->time_this('str_word_count',$a,array($str)); $bm->time_this('preg_split',$c,array($str)); $bm->time_this('reddit_split',$d,array($str)); $bm->time_this('scott_qw_raw',$e,array($str)); $bm->time_this('scott_qw_final',$f,array($str)); // Print out the summary of the benchmarks we've run $bm->summary();