Hidden php caching?

I don’t know if this is necessarily a PHP question, but…

I am running PHP 5.5.6 in XAMPP 1.8.3. I do not have the OpCache extension turned on. When benchmarking a page, I have noticed that the first time it loads usually requires significantly more time than the second time. After the first time, I always get the 2nd faster loading speed, unless I restart Apache (and sometimes a full computer reboot is needed). Why is this happening? Since I don’t seem to be using any caching (the scripts I am using do not cache anything, either, and it seems to affect all scripts) I am not sure why the second time I run a script it should be so much faster. Could Apache be doing some caching? Or maybe I am missing something in the php.ini? (did a search for ‘opc’ and ‘apc’ and found nothing there.)

The operating system or hardware (hard drive) has the data cached in memory. Linux web servers do this, too. The first load takes the longest, sometimes 10 times or more longer. It’s the same thing with programs such as Microsoft Excel. I noticed many years ago that starting Excel for the first time took a while. Closing it then starting it again a few minutes later was lightning fast. The data is loaded from cache instead of from disk.

I just tested starting GIMP. (On a quad core 3 GHZ CPU with 8 GB RAM.) Starting it up the first time took 19 seconds to fully start (the round thing to stop spinning). I closed it for a couple minutes then restarted. The second start took 9 seconds.

This long first load time is particularly noticeable on Wordpress and other scripts with large numbers of included files, especially on shared servers where the system is multi-tasking many other requests in addition to yours. Also more dramatic with mechanical hard drives than SSD.

Thanks, great information. Is there a way to force php to not use this - or I guess force windows (my platform) to not do this for certain files or directories? It is hard to profile my code if I am getting 120ms, then 20ms, then 0ms thereafter for each load.

Any caching optimization is going to be done at the hardware, OS, or software level. I know of no way to disable it. You are not the only one to be bothered by this. Search your favorite search engine for “php first load slow” and find plenty of other people saying the same thing.

http://stackoverflow.com/questions/10052186/first-page-request-on-website-very-slow

http://www.webhostingtalk.com/showthread.php?t=1096449

Open then close and then reopen any program on your computer that takes a few seconds to start initially. You will find the second load almost always is faster. The data must be cached somewhere.

Now that the shared server I am on is using SSD drives, this initial slowness is much less noticeable. Years ago I did write a small few line script that loaded another PHP file and output the time it took to do so. The first load was always much slower than subsequent loads and it seemed the data remained in cache somewhere for 30 minutes or longer before it reverted back to the slow initial load.

Any benchmarking on the initial load is not going to be representative of typical experience unless you have very little traffic and long periods of no page loads (or on a server maxing out memory). I would dismiss the initial load as an outlier.

That’s too bad. Thanks for the links, I will investigate them and see what I can turn up with additional searching. I will probably end up dismissing them as outliers, as you noted.

I wanted to stop back with the results of an old PHP script I wrote years ago to measure the load time of an included file. The script starts a timer, includes a second PHP file, stops the timer then outputs the time it took. All it does is measure the time to include another file. There is no other code to be processed. This is on a shared web server with SSD drives.

Load 1:
0.001134 seconds or 1.134 milliseconds

Load 2 a few seconds later:
5.8999999999976E-5 seconds or 0.058999999999976 milliseconds.

Load 3 about 2 minutes later:
Took: 6.6999999999928E-5 seconds or 0.066999999999928 milliseconds.

The initial load time is 19 times longer than the second load time. That is due to caching. The included file is cached somewhere and read from memory instead of the SSD drive on subsequent loads. When I was on a shared server with mechanical drives the first load time was usually in the 5 - 6 millisecond range. SSD drives are much faster at accessing data.

If you were to look at Wordpress on a mechanical drive with an average load time of 6 milliseconds per file include (may not achieve that average on a shared server with mechanical drives), that would be 80 file includes times 6 milliseconds, which would be 480 milliseconds or nearly half a second just to load all the data from the hard drive. That is why a load on a Wordpress site with little traffic can take more time on the first load. On overloaded server, nothing may exist in cache for long if at all so everything is slow.