Created
May 20, 2010 19:48
-
-
Save katanacrimson/407994 to your computer and use it in GitHub Desktop.
This file contains 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 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; | |
} |
Must be something to do with Win vs Linux too...you're in Windows right now, aren't you?
I'm running a 64bit build of PHP as well, that might make a difference.
Hmm, interesting.
I don't believe that Failnet (or anything that anyone adds to it) should be using source files over 1mb, so file() it is.
Eh, I need to find something large then. ext4 might prove better or worse for that, hard to say. You have that file so I can test it with that?
Alright, here's my results: http://gist.github.com/408354#file_benchmark.txt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oddly enough, when implementing the same sort of change that I made to the fgets() variant to the file() variant, file() ends up being faster.