Efficiency of reading and writing files and reading and writing databases in php _ PHP Tutorial

Source: Internet
Author: User
The efficiency of reading and writing files and reading and writing databases in php. This article will introduce a comparison between the efficiency of reading/writing files and reading/writing databases in php. For more information, see. The test procedure is as follows: Note 1: This article introduces the efficiency comparison between reading and writing files and reading and writing databases in php. For more information, see.

The test procedure is as follows:

// Note 1: Because the read database statement calls a simple encapsulation function twice, the read file is also changed to a sequential call twice, and the database record ID is 1 at the first entry, and the unique index.

The code is as follows:

// Note 2: 4 K data and integer data are tested twice.

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 result:
';
//---------------------------------
$ 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). 'second
';
//---------------------------------

$ 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). 'second
';
//---------------------------------

$ 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). 'second
';
//---------------------------------

$ Dbcon-> mydb_free_results ();
$ Dbcon-> mydb_disconnect ();

FnWriteCache('test.txt ', $ content );

Echo 'directly read the file test result:
';

//---------------------------------
$ 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).' second
';

Query Results of 4 K data:

Fetch_row 100000 times Time: 16.737720012665 seconds

Fetch_array 100000 times Time: 16.661195993423 seconds

Fetch_object 100000 requests Time: 16.775065898895 seconds

Test results of directly reading files:

File_get_contents reads 100000 times directly. Time: 5.4631857872009 seconds

Fopen direct read 100000 times Time: 11.463611125946 seconds

Integer ID query result:

Fetch_row 100000 times Time: 12.812072038651 seconds

Fetch_array 100000 times Time: 12.667390108109 seconds

Fetch_object 100000 requests Time: 12.988099098206 seconds

Test results of directly reading files:

File_get_contents reads 100000 times directly. Time: 5.6616430282593 seconds

Fopen direct read 100000 times Time: 11.542816877365 seconds


Test conclusion:

1. direct file reading is more efficient than Database query, and the connection and disconnection time are not counted yet.

2. the larger the content to be read at a time, the more obvious the advantage of direct file reading (the reading time increases slightly, which is related to the continuity of file storage and the cluster size ), this result is exactly the opposite of Tian Yuan's expectation, indicating that MYSQL may have attached some operations to read larger files (two times of increase by nearly 30% ), if the conversion is simple, the difference is small.

3. write files and INSERT can be inferred without testing, and the database efficiency will only be worse.

4. if you do not need to use the database features for a small configuration file, it is more suitable for storing it in an independent file without creating a separate data table or record, it is more convenient to store large files than to store files or music files. it is more reasonable to store index information such as paths or thumbnails in the database.

5. if you only read files in PHP, file_get_contents is more efficient than fopen and fclose, not including determining whether the function will take about 3 seconds.

6. fetch_row and fetch_object should be converted from fetch_array. Tianyuan has never seen the PHP source code. the execution of fetch_array alone indicates that fetch_array is more efficient, which seems the opposite to the online statement.


Bytes. The test procedure is as follows: // description 1: the read database...

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.