Skip to content

Instantly share code, notes, and snippets.

@katanacrimson
Created May 21, 2010 01:21
Show Gist options
  • Save katanacrimson/408354 to your computer and use it in GitHub Desktop.
Save katanacrimson/408354 to your computer and use it in GitHub Desktop.
obsidian@lithion-mint ~/Documents $ php benchmark.php
file - 0.413892 seconds
obsidian@lithion-mint ~/Documents $ php benchmark.php
file - 0.419085 seconds
obsidian@lithion-mint ~/Documents $ php benchmark.php
file - 0.428959 seconds
obsidian@lithion-mint ~/Documents $ php benchmark.php
file - 0.440547 seconds
obsidian@lithion-mint ~/Documents $ php benchmark.php
file - 0.430267 seconds
obsidian@lithion-mint ~/Documents $ php benchmark.php
fgets - 1.023405 seconds
obsidian@lithion-mint ~/Documents $ php benchmark.php
fgets - 0.999376 seconds
obsidian@lithion-mint ~/Documents $ php benchmark.php
fgets - 0.994365 seconds
obsidian@lithion-mint ~/Documents $ php benchmark.php
fgets - 1.025741 seconds
obsidian@lithion-mint ~/Documents $ php benchmark.php
fgets - 0.99858 seconds
C:\xampp\htdocs\Ximps>php tests.php
file - 0.310402 seconds
C:\xampp\htdocs\Ximps>php tests.php
file - 0.309679 seconds
C:\xampp\htdocs\Ximps>php tests.php
file - 0.314347 seconds
C:\xampp\htdocs\Ximps>php tests.php
file - 0.310647 seconds
C:\xampp\htdocs\Ximps>php tests.php
file - 0.311683 seconds
C:\xampp\htdocs\Ximps>php tests.php
fgets - 6.783487 seconds
C:\xampp\htdocs\Ximps>php tests.php
fgets - 6.762257 seconds
C:\xampp\htdocs\Ximps>php tests.php
fgets - 6.929103 seconds
C:\xampp\htdocs\Ximps>php tests.php
fgets - 6.747566 seconds
C:\xampp\htdocs\Ximps>php tests.php
fgets - 6.857936 seconds
./blob.php contained 20129 lines of source code....
<?php
$mode = 'fgets';
benchmark();
for($i = 0; $i <= 20; $i++)
{
if($mode == 'fgets')
getErrorContextFgets('./blob.php', 10000, 4);
elseif($mode == 'file')
getErrorContextFile('./blob.php', 10000, 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();
$content = file($file);
for ($i = ($line - $context); $i < (($line -1) + $context); $i++)
{
$return[] = $content[$i];
}
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment