The efficiency of reading and writing files in PHP and read and write databases

Source: Internet
Author: User

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.

The code is as follows Copy Code

Description 2: Test two times is 4K data, one is shaping data

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, the direct reading file compared to the database query efficiency is better, and the text has not counted on the connection and disconnect time.

2, the greater the content of one read, the more obvious the advantages of direct read file (read file time is a small increase, this is related to file storage continuity and cluster size, which is exactly the opposite of what the sky expects, indicating that MySQL may have additional operations attached to larger file reads (two times up nearly 30%), If only the simple assignment conversion should be a small difference.

3, write files and insert almost without testing can be measured, database efficiency will only be worse.

4, very small configuration file if you do not need to use the database characteristics, 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, the days have not seen the source of PHP, the single from the implementation can be explained Fetch_array efficiency is higher, this is contrary to the online version.

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.