- Set_time_limit (0);
- function Fnget ($filename)
- {
- $content = file_get_contents ($filename);
- return $content;
- }
- function Fngetcontent ($filename)
- {
- $content = Fnget ($filename);
- return $content;
- }
- $times = 100000;
- Echo ' Database query results:
';
- //---------------------------------
- $begin =fngetmicrotime ();
- for ($i =0; $i < $times; $i + +)
- {
- $res = $dbcon->mydb_query ("Select log_content from Blog WHERE log_id= ' 1 '");
- $row = $dbcon->mydb_fetch_row ($res);
- $content = $row [0];
- }
- Echo ' Fetch_row '. $times. ' Time: '. (Fngetmicrotime ()-$begin). ' Seconds
';
- //---------------------------------
- $begin =fngetmicrotime ();
- for ($i =0; $i < $times; $i + +)
- {
- $res = $dbcon->mydb_query ("Select log_content from Blog WHERE log_id= ' 1 '");
- $row = $dbcon->mydb_fetch_array ($res);
- $content = $row [' log_content '];
- }
- Echo ' Fetch_array '. $times. ' Time: '. (Fngetmicrotime ()-$begin). ' Seconds
';
- //---------------------------------
- $begin =fngetmicrotime ();
- for ($i =0; $i < $times; $i + +)
- {
- $res = $dbcon->mydb_query ("Select log_content from Blog WHERE log_id= ' 1 '");
- $row = $dbcon->mydb_fetch_object ($res);
- $content = $row->log_content;
- }
- Echo ' Fetch_object '. $times. ' Time: '. (Fngetmicrotime ()-$begin). ' Seconds
';
- //---------------------------------
- $dbcon->mydb_free_results ();
- $dbcon->mydb_disconnect ();
- Fnwritecache (' Test.txt ', $content);
- echo ' Direct read file test results:
';
- //---------------------------------
- $begin =fngetmicrotime ();
- for ($i =0; $i < $times; $i + +)
- {
- $content = fngetcontent (' test.txt ');
- }
- Echo ' file_get_contents direct read '. $times. ' Time: '. (Fngetmicrotime ()-$begin). ' Seconds
';
- //---------------------------------
- $begin =fngetmicrotime ();
- for ($i =0; $i < $times; $i + +)
- {
- $fname = ' test.txt ';
- if (file_exists ($fname))
- {
- $FP =fopen ($fname, "R");//flock ($fp, LOCK_EX);
- $file _data=fread ($fp, FileSize ($fname));//rewind ($FP);
- Fclose ($FP);
- }
- $content = fngetcontent (' test.txt ');
- }
- Echo ' fopen direct read '. $times. ' Time: '. (Fngetmicrotime ()-$begin). ' Seconds
';
Copy Code4K size data Query results: Fetch_row 100,000 time: 16.737720012665 seconds Fetch_array 100,000 times: 16.661195993423 seconds fetch_object 100000 Time: 16.775065898895 seconds Direct read file test result: file_get_contents Direct read 100,000 time: 5.4631857872009 second fopen Direct read 100,000 time: 11.463611125946 seconds Reshape ID Query Result: Fetch_row 100,000 time: 12.812072038651 seconds Fetch_array 100,000 times: 12.667390108109 seconds fetch_object 100000 Time: 12.988099098206 seconds Direct read file test result: file_get_contents Direct read 100,000 time: 5.6616430282593 second fopen Direct read 100,000 time: 11.542816877365 seconds Test conclusion: 1, the direct reading of the file compared to the database query efficiency is better, and the text has not been counted on the connection and disconnection time. 2, the larger the content of a read, the more obvious advantages of the direct read file (read file time is a small increase, which is related to the continuity of file storage and cluster size, etc.), the result is exactly the opposite of the forecast, indicating that MySQL to the larger file read may have added some operations (two times increased by nearly 30%), If only the simple assignment conversion should be small difference. 3, write files and insert almost without testing can be measured, database efficiency will only be worse. 4, very small configuration files if you do not need to use the database features, more suitable for separate files to access, do not have to create a separate data table or record, a large file than tablets, music and other file storage more convenient, only the path or thumbnails and other index information in the database more reasonable. 5, PHP If only read the file, file_get_contents than fopen, fclose more efficient, not including the determination of the existence of this function time will be less than 3 seconds. 6, Fetch_row and Fetch_object should be from the Fetch_array conversion from, I have not read the source code of PHP, single from the implementation can be explained that fetch_array more efficient, which is contrary to the online statement. Assuming that the program efficiency and critical processes are equivalent and not counted as caching, the read-write data of any type does not directly manipulate the file, regardless of the MSYQL process, and finally to the disk to read the "file" (record storage equivalent), so of course, all of this premise is read-only content, Regardless of any sort or find operation. |