Skip to content

Instantly share code, notes, and snippets.

@katanacrimson
Created May 20, 2010 19:48
Show Gist options
  • Save katanacrimson/407994 to your computer and use it in GitHub Desktop.
Save katanacrimson/407994 to your computer and use it in GitHub Desktop.
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 $
<?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;
}
@katanacrimson
Copy link
Author

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.

@katanacrimson
Copy link
Author

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.

@katanacrimson
Copy link
Author

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.

@katanacrimson
Copy link
Author

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?

@katanacrimson
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment