Created
May 20, 2010 19:48
-
-
Save katanacrimson/407994 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| obsidian@lithion-mint ~/Documents $ php benchmark.php | |
| file - 0.212994 seconds | |
| obsidian@lithion-mint ~/Documents $ php benchmark.php | |
| file - 0.204549 seconds | |
| obsidian@lithion-mint ~/Documents $ php benchmark.php | |
| file - 0.200732 seconds | |
| obsidian@lithion-mint ~/Documents $ php benchmark.php | |
| file - 0.207176 seconds | |
| obsidian@lithion-mint ~/Documents $ php benchmark.php | |
| file - 0.210159 seconds | |
| obsidian@lithion-mint ~/Documents $ php benchmark.php | |
| fgets - 0.188541 seconds | |
| obsidian@lithion-mint ~/Documents $ php benchmark.php | |
| fgets - 0.163778 seconds | |
| obsidian@lithion-mint ~/Documents $ php benchmark.php | |
| fgets - 0.163242 seconds | |
| obsidian@lithion-mint ~/Documents $ php benchmark.php | |
| fgets - 0.164689 seconds | |
| obsidian@lithion-mint ~/Documents $ php benchmark.php | |
| fgets - 0.166871 seconds | |
| obsidian@lithion-mint ~/Documents $ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $mode = 'fgets'; | |
| benchmark(); | |
| for($i = 0; $i <= 2000; $i++) | |
| { | |
| try | |
| { | |
| // blah blah 123 | |
| throw new Exception('test'); | |
| } | |
| catch(Exception $e) | |
| { | |
| if($mode == 'fgets') | |
| getErrorContextFgets($e->getFile(), $e->getLine(), 4); | |
| elseif($mode == 'file') | |
| getErrorContextFile($e->getFile(), $e->getLine(), 4); | |
| } | |
| } | |
| echo $mode . ' - '; | |
| benchmark(); | |
| die(); | |
| function getTime() | |
| { | |
| $timer = explode(' ', microtime()); | |
| return $timer[1] + $timer[0]; | |
| } | |
| function benchmark() | |
| { | |
| static $start = 0; | |
| if(!$start) | |
| $start = getTime(); | |
| else | |
| echo round(getTime() - $start, 6) . ' seconds' . PHP_EOL; | |
| } | |
| function getErrorContextFile($file, $line, $context = 3) | |
| { | |
| $return = array(); | |
| foreach (file($file) as $i => $str) | |
| { | |
| if (($i + 1) < ($line + $context) && ($i + 1) > ($line - $context)) | |
| { | |
| $return[] = $str; | |
| } | |
| } | |
| return $return; | |
| } | |
| function getErrorContextFgets($file, $line, $context = 3) | |
| { | |
| $return = ''; | |
| $line_i = 0; | |
| if($fh = fopen($file,"r")) | |
| { | |
| while (!feof($fh)) | |
| { | |
| $line_i++; | |
| if($line_i > ($line - $context)) | |
| { | |
| if($line_i < ($line + $context)) | |
| break; | |
| $return[] = fgets($fh); | |
| } | |
| else | |
| { | |
| fgets($fh); | |
| } | |
| } | |
| fclose($fh); | |
| } | |
| return $return; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alright, here's my results: http://gist.github.com/408354#file_benchmark.txt