Monday, March 14, 2016

Crazy PHP files

Playing around with files and streams on PHP 5, here is a very weird behaviour.

Lets take a 123Mb file, on Windows 10, on a common quad core computer with a "normal" hard disk.

<code>
$f = fopen($filename, 'rb');
while (!feof($f))
{
   $data = fread($f, 4096);
}
</code>

Will take approx 0.1 sec to load the totality of the file in memory.

Now, if you fseek the pointer jumping 4096 bytes between each read.
This should, tecnically talking, take half the time since you jump half the data and have (theorycally) half of bytes transfered from HD to memory.

<code>
$f = fopen($filename, 'rb');
$num = 0;
while (!feof($f))
{
  fseek($f, $num*8192);
   $data = fread($f, 4096);
  $num++;
}
</code>

Well.. Guess what.
It just take 4 seconds. 40 times more time !
Avoid as much as possible fseek on PHP. and windows.

On linux system it seems it works fine as expected (tried on a Centos6-64 bits)

curve of size of read block vs time is like a U, with optimum time between 2K-8K size.
The global loading time is a mix between quantity of reads, seeks and time for memory allocation

Notes:
- Have to try with some differents sizes of blocks and implement same stuff in C++ to try out.

---





No comments:

Post a Comment