Efficiency Comparison between reading and writing files and reading and writing databases in php _ PHP Tutorial

Source: Internet
Author: User
In php, the efficiency of reading and writing files is compared with that of reading and writing databases. This problem was also recently mentioned, that is, whether to read files faster or to read databases faster and how fast it can be. tian yuan also searched for the problem and did not see any netizens reply to this question, it may also be that this question was just recently come to mind, that is, whether to read files faster or read databases faster, how fast it can be, Tianyuan also searched, and no netizens answered this question, it may also be too simple. in this article, we will test whether VC has not been installed due to the time relationship. Tianyuan first tested it with PHP, next time, I will try again in C/C ++. because PHP's underlying parsing should also be based on C, it is estimated that the test results of both environments are similar, and there will be a big gain in small problems, now let's take a look at the test process and results.

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 continuous call for two times. the database record ID is 1 at the first entry and has a unique index.
Note 2: 4 K data and integer data are tested twice.

The 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 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. I have not read the PHP source code. the execution alone shows that fetch_array is more efficient, which is opposite to the online statement.
In fact, before doing this test, you will probably get the result from your personal experience. after the test is completed, you will feel a new sense of openness. It is assumed that, when the program efficiency and key process are equivalent and not included in the cache, no data of any type can be read and written directly, regardless of the MSYQL process, finally, you must read the "file" on the disk (equivalent to the record storage area). Therefore, the premise of all this is read-only content, regardless of any sort or search operations.

...

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.