PHP: Require/Include vs Autoloader PHP: Require/Include vs Autoloader

Google has long since ingrained into my brain how important every millisecond is when dealing with large amounts of traffic.

In this post, I'm going to demonstrate a really simplistic way to improve your PHP website performance.  It seems to go against the grain of "old school" vanilla PHP writing, but the results are incredible!  By removing the use of require and include and replacing it with a spl_autoload_register function instead, the time savings are more than 10 times!

Not only that, in theory it's less lines of code!


To start, let's create three basic PHP files that contain empty class definitions.  They are called: class1.php, class2.php, and class3.php.  I've grouped them below in one code listing, but these should be three separate files:


<?php
class Class1 {
}
?>
<?php
class Class2 {
 
}
?>
<?php
class Class3 {
 
}
?>

Now I am going to create two similar files.  These files simply create the three new classes in a for loop.  The first file I called require.php:


<?php
$start = microtime();
 
for ($x = 0; $x < 500; $x++) {
require_once 'class1.php';
require_once 'class2.php';
require_once 'class3.php';
 
$class1 = new Class1();
$class2 = new Class2();
$class3 = new Class3();
}
 
$end = microtime();
?>
<hr/>
Execution Time: <?php echo ($end - $start) ;?> seconds.

The second file is called autoloader.php:


<?php
$start = microtime();
 
spl_autoload_register('autoload');
 
for ($x = 0; $x < 500; $x++) {
$class1 = new Class1();
$class2 = new Class2();
$class3 = new Class3();
}
 
$end = microtime();
?>
<hr/>
Execution Time: <?php echo ($end - $start) ;?> seconds.
 
<?php
/**
* Loads a class.
* @param string $className The name of the class to load.
*/
function autoload($className) {
require_once strtolower($className) . '.php';
}

As you may notice, the only significant difference is at the top of this file a call is made to spl_autoload_register tell PHP how to autoload the classes.  In this scenario I tell it to call autoload function.  This function performs a lower case on the class name and requires the file.

It's important to note, that a consistent naming convention is required to automatically load the classes.  Otherwise, an alternative approach can be taken which uses a class and a static array of key/value pairs that associate all of your class names with the appropriate file to include.

Now for the results; several posts ago I created and released a speed tester on Git: https://github.com/endyourif/speedtester

Using this application, I ran it twice – once for each page 100 times.  Once the app is done, it drops the highest and lowest times than calculates an average.

Prepare to be amazed.

The average run time for the require.php file was 7 milliseconds.

Drum roll please…

The average run time for the autoloader.php file was 1 milliseconds.

Because this is all running locally and doing no real processing, the times are extremely low; however, if in this scenario there is a 7 times speed improvement!

Summary

You might notice of course that in the require.php file I am performing the require_once inside the for loop.  Obviously this isn't how something should be developed.  The idea here is that, in typical code when you have multiple files including and referencing each other, you are bound to attempt to include the same file multiple times.

When this happens, as the demo above shows, performance decreases drastically while PHP checks to see if that file has already been loaded and exists versus simply determining if the class already exists.

To download the files in examples above, view my Git repo here: https://github.com/endyourif/RequireVsAutloader

 

Published on Aug 29, 2012

Tags: Optimization | php | require_once | require | include | include_once | spl_autoload_register | PHP

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.