note 91414 added to function.file-get-contents

From: Date: Tue, 09 Jun 2009 21:35:20 +0000
Subject: note 91414 added to function.file-get-contents
Groups: php.notes 
Request: Send a blank email to php-notes+get-156670@lists.php.net to get a copy of this message
In my dev environment with a relatively low-speed drive (standard SATA 7200RPM) reading a 25MB zip file in 10 times... <? $data = cat /tmp/test.zip; // 1.05 seconds $fh = fopen('/tmp/test.zip', 'r'); $data = fread($fh, filesize('/tmp/test.zip')); fclose($fh); // 1.31 seconds $data = file_get_contents('/tmp/test.zip'); // 1.33 seconds ?> However, on a 21k text file running 100 iterations... <? $data = cat /tmp/test.txt; // 1.98 seconds $fh = fopen('/tmp/test.txt', 'r'); $data = fread($fh, filesize('/tmp/test.txt')); fclose($fh); // 0.00082 seconds $data = file_get_contents('/tmp/test.txt'); // 0.0069 seconds ?> Despite the comment about file_get_contents being faster do to memory mapping, file_get_contents is slowest in both of the above examples. If you need the best performance out of your production box, you might want to throw together a script to check out which method is fastest for what size files on that particular machine, then optimize your code to check the file size and use the appropriate function for it. ---- Server IP: 64.71.164.2 Probable Submitter: 72.177.49.202 ---- Manual Page -- http://www.php.net/manual/en/function.file-get-contents.php Edit -- https://master.php.net/note/edit/91414 Del: integrated -- https://master.php.net/note/delete/91414/integrated Del: useless -- https://master.php.net/note/delete/91414/useless Del: bad code -- https://master.php.net/note/delete/91414/bad+code Del: spam -- https://master.php.net/note/delete/91414/spam Del: non-english -- https://master.php.net/note/delete/91414/non-english Del: in docs -- https://master.php.net/note/delete/91414/in+docs Del: other reasons-- https://master.php.net/note/delete/91414 Reject -- https://master.php.net/note/reject/91414 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#156670) next »