Comparison between read-write files and read-write database efficiency in PHP share _php tutorial

Source: Internet
Author: User
This problem is also recently thought, that is to read the file faster or faster to read the database, how much faster, days edge also searched, did not see a netizen on this question answered, may also be too simple reason, we this article or to measure, because of the time, VC has not installed, Day edge first with PHP test a bit, The next time you can add a test to the text/C + + in this article, because the underlying parsing of PHP should also be based on C, so it is estimated that the environmental test results are similar, small problems big harvest, now look at the test process and results.

The test procedure is as follows:

Note 1: Because the Read database statement calls a simple packet function two times, so the read file is also changed to a continuous call two times, the database record ID 1 is in the first, and a unique index.
Illustration 2: Test two times is 4K data, once is the shaping data

Copy CodeThe code is 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:
';
//---------------------------------
$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
';

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

Test conclusion:

1, directly read the file compared to the database query efficiency is better, and the text is not 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.
In fact, before doing this experiment, judging from personal experience has a general result, after the test is a kind of enlightened feeling. 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.

http://www.bkjia.com/PHPjc/824895.html www.bkjia.com true http://www.bkjia.com/PHPjc/824895.html techarticle This problem is also recently thought, that is, whether to read the document faster or read the database faster, how much faster, the sky has also searched, did not see a netizen on this question has been answered, may be ...

  • 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.