The efficiency of reading and writing files in PHP and read-write database sharing _php skills

Source: Internet
Author: User

This question is also only recently thought of, is to read the file faster or read the database faster, can quickly how much, the days are also searched, did not see a netizen to answer this question, it may be too simple sake, we have to test this article, because of the time, the VC has not installed, the sky Edge first tested with PHP, The next time in C/s + + to supplement the test to this article, because the underlying parsing of PHP should also be based on C, so the two environmental test results are estimated to be similar, small problems big harvest, now look at the test process and results.

The test procedure is as follows:

Illustration 1: Since the Read database statement calls the simple packet function two times, the read file is also changed to two consecutive times, the database record ID is 1 in the first, and the unique index.
Description 2: Test two times is 4K data, one is shaping data

Copy Code code as follows:

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:<br/> ';
//---------------------------------
$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: <font color=red> '. (Fngetmicrotime ()-$begin). ' </font> sec <br/> ';
//---------------------------------
$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: <font color=red> '. (Fngetmicrotime ()-$begin). ' </font> sec <br/> ';
//---------------------------------
$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: <font color=red> '. (Fngetmicrotime ()-$begin). ' </font> sec <br/> ';
//---------------------------------
$dbcon->mydb_free_results ();
$dbcon->mydb_disconnect ();
Fnwritecache (' Test.txt ', $content);
echo ' Direct read file test results:<br/> ';
//---------------------------------
$begin =fngetmicrotime ();
for ($i =0; $i < $times; $i + +)
{
$content = fngetcontent (' test.txt ');
}
Echo ' file_get_contents read directly '. $times. ' Time: <font color=red> '. (Fngetmicrotime ()-$begin). ' </font> sec <br/> ';
//---------------------------------
$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 read directly '. $times. ' Time: <font color=red> '. (Fngetmicrotime ()-$begin). ' </font> sec <br/> ';

Query results for 4K size data:
Fetch_row 100,000 times: 16.737720012665 seconds
Fetch_array 100,000 times: 16.661195993423 seconds
Fetch_object 100,000 times: 16.775065898895 seconds
Direct read file test results:
File_get_contents Direct Read 100,000 times: 5.4631857872009 seconds
Fopen Direct Read 100,000 times: 11.463611125946 seconds
Shaping ID Query Results:
Fetch_row 100,000 times: 12.812072038651 seconds
Fetch_array 100,000 times: 12.667390108109 seconds
Fetch_object 100,000 times: 12.988099098206 seconds
Direct read file test results:
File_get_contents Direct Read 100,000 times: 5.6616430282593 seconds
Fopen Direct Read 100,000 times: 11.542816877365 seconds

Test conclusion:

1, direct read file compared to database query efficiency is better, and the text has not counted on the connection and disconnect time.
2, the larger the content of one read, the more obvious the advantages of direct read files (read file time is a small increase, which is related to file storage continuity and cluster size, etc.), the result is precisely the opposite of the expected fate, It shows that MySQL may have additional operations attached to larger file reads (two times increased by nearly 30%), if only the simple assignment conversion should be a small difference.
3, write files and inserts can be measured almost without testing, database efficiency will only be worse.
4, very small configuration file if you do not need to use the database attribute, more suitable for access to separate files, no need to create a separate data table or records, large files such as pictures, music, etc., using file storage is more convenient, only the path or thumbnails and other index information into the database more reasonable.
5, PHP if only read files, file_get_contents than fopen, fclose more efficient, does not include the judgment of the existence of this function time will be less than 3 seconds.
6, Fetch_row and Fetch_object should be converted from the Fetch_array, I have not read the source code of PHP, single from the implementation can be explained that fetch_array efficiency is higher, this is contrary to the online version.
In fact, before doing this experiment, from personal experience to have the approximate results, the test is done after the feeling of the enlightened. Assuming that the procedures are efficient and critical processes are equivalent and do not count into caching, reading or writing any type of data does not directly manipulate the file to quickly, regardless of the MSYQL process, and finally to the disk to read the "file" (Record store equivalent), so of course all of this premise is read-only content, Unrelated to any sort or lookup operation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.