The static content was 10 paragraphs of lorem ipsum. The results are shown to two decimal places.
| Iterations | Include time | file_get_content time | difference (in seconds) |
|---|---|---|---|
| 100 | 0.50 | 0.44 | 0.06 |
| 1000 | 6.70 | 6.01 | 0.69 |
| 10000 | 55.12 | 50.55 | 4.57 |
That is an average 10% increase in execution time by using include to serve static content.
The benchmark driver:
<?php
/**
* Benchmarking include vs file_get_contents for serving
* static content.
*
* @author : Ed Cradock
* @require timer
*/
define('ITERATION_OFFSET', 10000);
require_once 'class/timer.php';
$timer = new timer();
/*
* Time include
*/
$timer->start();
for($i = 0; $i <= ITERATION_OFFSET; $i++)
{
include 'benchmark_content.txt';
}
$timer->finish();
$dump_include = $timer->result();
// Reset the timer
$timer->reset();
/*
* Time file_get_contents
*/
$timer->start();
for($i = 0; $i <= ITERATION_OFFSET; $i++)
{
echo file_get_contents('benchmark_content.txt');
}
$timer->finish();
echo 'Benchmark of include : ', $dump_include, "\n";
echo 'Benchmark of file_get_contents : ', $timer->result(), "\n";
?>
<?php
/**
* Basic timer class
* Returns value in seconds
* @name : timer
* @author : Ed Cradock
*/
class timer
{
/**
* Start time
* @var float
* @access private
*/
private $start;
/**
* End time
* @var float
* @access private
*/
private $end;
/**
* Class constructor
*
* @access public
*/
public function __construct()
{
$this->reset();
}
/**
* Start the timer
*
* @access public
* @param foo $foo - bar
* @return void
*/
public function start()
{
$this->start = microtime(true);
}
/**
* Finish the timer
*
* @access public
* @return void
*/
public function finish()
{
$this->finish = microtime(true);
}
/**
* Returns the elapsed time in seconds (not rounded off)
*
* @access public
* @return float
*/
public function result()
{
return ($this->finish - $this->start);
}
/**
* Reset the timer
*
* @access public
* @return void
*/
public function reset()
{
$this->start = 0;
$this->finish = 0;
}
}
?>