Comparison and analysis of the efficiency of php file read/write and database read/write

Source: Internet
Author: User
Comparison and analysis of the efficiency of php file read/write and database read/write

  1. Set_time_limit (0 );
  2. Function fnGet ($ filename)
  3. {
  4. $ Content = file_get_contents ($ filename );
  5. Return $ content;
  6. }
  7. Function fnGetContent ($ filename)
  8. {
  9. $ Content = fnGet ($ filename );
  10. Return $ content;
  11. }
  12. $ Times = 100000;
  13. Echo 'database query result:
    ';
  14. //---------------------------------
  15. $ Begin = fnGetMicroTime ();
  16. For ($ I = 0; $ I <$ times; $ I ++)
  17. {
  18. $ Res = $ dbcon-> mydb_query ("SELECT log_Content FROM blog WHERE log_ID = '1 '");
  19. $ Row = $ dbcon-> mydb_fetch_row ($ res );
  20. $ Content = $ row [0];
  21. }
  22. Echo 'fetch _ row'. $ times. 'time:'. (fnGetMicroTime ()-$ begin). 'second
    ';
  23. //---------------------------------
  24. $ Begin = fnGetMicroTime ();
  25. For ($ I = 0; $ I <$ times; $ I ++)
  26. {
  27. $ Res = $ dbcon-> mydb_query ("SELECT log_Content FROM blog WHERE log_ID = '1 '");
  28. $ Row = $ dbcon-> mydb_fetch_array ($ res );
  29. $ Content = $ row ['log _ content'];
  30. }
  31. Echo 'fetch _ array'. $ times. 'time:'. (fnGetMicroTime ()-$ begin). 'second
    ';
  32. //---------------------------------
  33. $ Begin = fnGetMicroTime ();
  34. For ($ I = 0; $ I <$ times; $ I ++)
  35. {
  36. $ Res = $ dbcon-> mydb_query ("SELECT log_Content FROM blog WHERE log_ID = '1 '");
  37. $ Row = $ dbcon-> mydb_fetch_object ($ res );
  38. $ Content = $ row-> log_Content;
  39. }
  40. Echo 'fetch _ object'. $ times. 'time:'. (fnGetMicroTime ()-$ begin). 'second
    ';
  41. //---------------------------------
  42. $ Dbcon-> mydb_free_results ();
  43. $ Dbcon-> mydb_disconnect ();
  44. FnWriteCache('test.txt ', $ content );
  45. Echo 'directly read the file test result:
    ';
  46. //---------------------------------
  47. $ Begin = fnGetMicroTime ();
  48. For ($ I = 0; $ I <$ times; $ I ++)
  49. {
  50. $ Content = fnGetContent('test.txt ');
  51. }
  52. Echo 'File _ get_contents direct read '. $ times.' time: '. (fnGetMicroTime ()-$ begin).' seconds
    ';
  53. //---------------------------------
  54. $ Begin = fnGetMicroTime ();
  55. For ($ I = 0; $ I <$ times; $ I ++)
  56. {
  57. $ Fname = 'test.txt ';
  58. If (file_exists ($ fname ))
  59. {
  60. $ Fp = fopen ($ fname, "r"); // flock ($ fp, LOCK_EX );
  61. $ File_data = fread ($ fp, filesize ($ fname); // rewind ($ fp );
  62. Fclose ($ fp );
  63. }
  64. $ Content = fnGetContent('test.txt ');
  65. }
  66. Echo 'fopen direct Read'. $ times. 'Time: '. (fnGetMicroTime ()-$ begin).' second
    ';

Query result of 4 K data size: fetch_row 100000 times Time: 16.737720012665 seconds fetch_array 100000 times Time: 16.661195993423 seconds fetch_object 100000 times Time: 16.775065898895 seconds

Test result of direct file reading: file_get_contents reads 100000 times directly. Time: 5.4631857872009 seconds. fopen reads 100000 times directly. 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 times Time: 12.988099098206 seconds

Test result of direct file reading: file_get_contents reads 100000 times directly. Time: 5.6616430282593 seconds. fopen reads 100000 times directly. 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. 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.