Which method is least expensive on the server? I am dynamically creating directories and would like each of them to contain an empty html file. Also how could one measure the difference?
$file = text.html;
$newDest = myDir/text.html;
copy($file, $newDest);
VS
$File = "myDir/text.html";
$Handle = fopen($File, 'w');
$Data = '';
fwrite($Handle, $Data);
print "Data Written";
fclose($Handle);
Copy operations are generally very expensive for a variety of reasons. I ran the following operations each 1000 times to compare them:
touchis consistently twice as fast as anyfopenmethod (all were around the same speed).copywas always the slowest.In terms of memory,
fopenprobably uses more since you have to store the file handle whiletouchandcopyonly return booleans.In summary, use
touch.