Comparison of efficiency between read-write files and read-write database in PHP

Source: Internet
Author: User
  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 results:
    ';
  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). ' Seconds
    ';
  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). ' Seconds
    ';
  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). ' Seconds
    ';
  41. //---------------------------------
  42. $dbcon->mydb_free_results ();
  43. $dbcon->mydb_disconnect ();
  44. Fnwritecache (' Test.txt ', $content);
  45. echo ' Direct read file test results:
    ';
  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). ' Seconds
    ';
Copy Code

4K size data Query results: Fetch_row 100,000 time: 16.737720012665 seconds Fetch_array 100,000 times: 16.661195993423 seconds fetch_object 100000 Time: 16.775065898895 seconds

Direct read file test result: file_get_contents Direct read 100,000 time: 5.4631857872009 second fopen Direct read 100,000 time: 11.463611125946 seconds

Reshape ID Query Result: Fetch_row 100,000 time: 12.812072038651 seconds Fetch_array 100,000 times: 12.667390108109 seconds fetch_object 100000 Time: 12.988099098206 seconds

Direct read file test result: file_get_contents Direct read 100,000 time: 5.6616430282593 second fopen Direct read 100,000 time: 11.542816877365 seconds

Test conclusion: 1, the direct reading of the file compared to the database query efficiency is better, and the text has not been 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. 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.

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