Profiling my scripts, the biggest drain on performance is the autoloader, specifically the require/include line.
A current site includes 38 files.
I did a quick test using include/require/require once/include once. Profiling just the autoload function and changing only the include/require line here’s what I got just as a very basic test.
[table=“width: 500”]
[tr]
[td]require_once[/td]
[td]124.75ms[/td]
[/tr]
[tr]
[td]require[/td]
[td]116.64ms[/td]
[/tr]
[tr]
[td]include_once[/td]
[td]120.56ms[/td]
[/tr]
[tr]
[td]include[/td]
[td]119.24ms[/td]
[/tr]
[/table]
All very close. Nothing really in it. However, 38 calls to include/require are using 70% of my script’s CPU time so it’s a hefty chunk of resources and easily the most resource intensive function I have.
Now that still seemed a lot to me but perhaps it just a file I/O bottleneck. As a basic test, I tried:
eval('?>' . file_get_contents($file));
instead of
require $file;
The result… 24.29ms roughly five times faster. This is rather astonishing. obviously eval is bad™ but this proves that include/require are doing a hell of a lot more than simply loading the file. By using eval(file_get_contents()) I’ve improved my entire script performance by almost 100%.
Any idea why include should be so comparatively slow?
edit: the files being included are doing no real processing on include, they are all just class definitions.