Windows – PHP File access very slow in windows

hard drivePHPspeedssdwindows 10

I recently noticed, that my development server set up on Windows 10 is slow as hell.
I did some debugging/profiling (Prestashop 1.6) with xdebug and culprint was file_exists. Script load time was ~43s from what file_exist took ~40s.

I wrote a small test, to see how performance would be on my Windows 10 machine and old Centos7 laptop. Here is the script:

<?php
$microtime = microtime(true);
function displayCounter($txt){
    global $microtime;
echo "<p> {$txt}: ". round((microtime(true) - $microtime) * 1000,0) ."ms</p>";

    $microtime = microtime(true);
}

$file_prefix = __DIR__ . '/file';

for($i= 1; $i < 1000; $i++){
    file_put_contents($file_prefix . $i, '');
}
displayCounter('File Creation');

for($i= 1; $i < 1000; $i++){
    file_exists($file_prefix . $i);
}
displayCounter('file_exists');

clearstatcache();

for($i= 1; $i < 1000; $i++){
    file_exists($file_prefix . $i);
}
displayCounter('file_exists (after cache clear)');


for($i= 1; $i < 1000; $i++){
    unlink($file_prefix . $i);
}
displayCounter('unlink');

Windows server is based on SSD drive and result is:

File Creation: 1992ms
file_exists: 1055ms
file_exists (after cache clear): 963ms
unlink: 696ms

And linux (laptop with 5400rpm HDD drive)

File Creation: 226ms
file_exists: 5ms
file_exists (after cache clear): 4ms
unlink: 50ms

I think something is wrong here, but dont know what yet – so I came here for help.
Can't solve what's the issue here. Any "service" I should look for? Like Defender (which is disabled in my case)

EDIT

Did some more testing with same script. Repated it few times to check and results are below. Directory from which you call script matters. Still can't find what breaks it (except I know it's Windows…)

All tests (except M.2 Drive) are good when current cmd path is set where work is being done.

C: M.2
H: SSD
D: SSDHD
F: / E: HDD

(GPT) Current Folder C:\ (M.2)
D:\test_file_exists\test.php (SSDHD)
File Creation: 2541ms
file_exists: 1188ms
file_exists (cached in theory): 986ms
file_exists (after cache clear): 879ms
unlink: 1361ms

(GPT) Current Folder C:\ (M.2)
E:\test_file_exists\test.php (HDD)
File Creation: 2617ms
file_exists: 1031ms
file_exists (cached in theory): 893ms
file_exists (after cache clear): 841ms
unlink: 1156ms

(MBR) Current Folder C:\ (M.2)
C:\test_file_exists\test.php (M.2)
File Creation: 2485ms
file_exists: 2283ms
file_exists (cached in theory): 2045ms
file_exists (after cache clear): 2053ms
unlink: 1191ms

(MBR) Current Folder C:\ (M.2)
F:\test_file_exists\test.php (HDD)
File Creation: 2274ms
file_exists: 1080ms
file_exists (cached in theory): 922ms
file_exists (after cache clear): 838ms
unlink: 1105ms

(MBR) Current Folder C:\ (M.2)
H:\test_file_exists\test.php (SSD)
File Creation: 2243ms
file_exists: 1024ms
file_exists (cached in theory): 860ms
file_exists (after cache clear): 903ms
unlink: 1134ms

=============== TESTS STARTED IN ROOT OF VOLUME

(GPT) Current Folder D:\ (SSDHD)
D:\test_file_exists\test.php (SSDHD)
File Creation: 2277ms
file_exists: 1056ms
file_exists (cached in theory): 904ms
file_exists (after cache clear): 897ms
unlink: 1135ms

(GPT) Current Folder E:\ (HDD)
E:\test_file_exists\test.php (HDD)
File Creation: 2436ms
file_exists: 1116ms
file_exists (cached in theory): 844ms
file_exists (after cache clear): 849ms
unlink: 1145ms

(MBR) Current Folder C:\ (M.2)
C:\test_file_exists\test.php (M.2)
File Creation: 2311ms
file_exists: 2115ms
file_exists (cached in theory): 1986ms
file_exists (after cache clear): 1969ms
unlink: 1168ms

(MBR) Current Folder F:\ (HDD)
F:\test_file_exists\test.php (HDD)
File Creation: 2365ms
file_exists: 1031ms
file_exists (cached in theory): 961ms
file_exists (after cache clear): 849ms
unlink: 1112ms

(MBR) Current Folder H:\ (SSD)
H:\test_file_exists\test.php (SSD)
File Creation: 2251ms
file_exists: 1066ms
file_exists (cached in theory): 873ms
file_exists (after cache clear): 835ms
unlink: 1137ms
=============== TESTS STARTED IN FOLDER OF SCRIPT

(GPT) Current Folder D:\test_file_exists (SSDHD)
D:\test_file_exists\test.php (SSDHD)
File Creation: 1279ms
file_exists: 61ms
file_exists (cached in theory): 60ms
file_exists (after cache clear): 51ms
unlink: 1873ms

(GPT) Current Folder E:\test_file_exists (HDD)
E:\test_file_exists\test.php (HDD)
File Creation: 1395ms
file_exists: 68ms
file_exists (cached in theory): 59ms
file_exists (after cache clear): 48ms
unlink: 1183ms

(MBR) Current Folder C:\test_file_exists (M.2)
C:\test_file_exists\test.php (M.2)
File Creation: 1410ms
file_exists: 1261ms
file_exists (cached in theory): 1293ms
file_exists (after cache clear): 1298ms
unlink: 1129ms

(MBR) Current Folder F:\test_file_exists (HDD)
F:\test_file_exists\test.php (HDD)
File Creation: 1285ms
file_exists: 58ms
file_exists (cached in theory): 61ms
file_exists (after cache clear): 57ms
unlink: 1639ms

(MBR) Current Folder H:\test_file_exists (SSD)
H:\test_file_exists\test.php (SSD)
File Creation: 1365ms
file_exists: 72ms
file_exists (cached in theory): 55ms
file_exists (after cache clear): 62ms
unlink: 1092ms

Best Answer

The poster now reports that his problem has disappeared after, according to my suggestion, he booted in Safe mode.

It does sometimes happen that running in Safe mode lets Windows sort out its problem. It also sometimes happen that rebooting a couple of times fixes a problem. Maybe it was the combination of both that helped here.

It will be hilarious if the problem was fixed by a side-effect of my suggestion, by accident.

This should not detract from the validity of the procedure I have outlined in my original answer below, intended to detect a troublesome product which is disabled when booting in Safe mode.


Original answer

Everything works correctly when booted in Safe Mode With Networking. In this mode, Windows starts with only core drivers plus networking support, and especially without launching on startup any third-party applications or drivers.

Your slow-down is therefore most likely caused by some product that is installed on the computer. You will therefore need to locate and disable it.

I don't know if the slow-down is only limited to PHP. In the rest of my answer I will treat the general Windows case. But if the problem is only limited to PHP, this might be caused by some add-on to PHP itself.

The best product that will help in finding a problem with an installed product is autoruns, with which you can turn startup products off or back on with one click.

See this article for information on using autoruns :
Using Autoruns to Deal with Startup Processes and Malware.

Autoruns will display all startup products in its Everything tab. I suggest to study the list for anything which could possibly ring a bell. If nothing comes to mind, you could use brute-force by turning off blocks of products and rebooting each time, refining the number of disabled products until the product is found.

Once you find the product, it is your choice whether to uninstall it for good, or look for a newer version, or contact the developer. We are also here for help.

Related Question