Php ultra-fast and efficient statistics on the number of large file lines, _ PHP Tutorial

Source: Internet
Author: User
Php uses ultra-fast and efficient statistics on the number of large file lines ,. Php uses ultra-fast and efficient statistics on the number of large file lines. php is used to obtain the number of file lines. The answer provided on the internet is usually one-time reading like file, which is not applicable to large files. Usually large files, large php, ultra-fast and efficient statistics on the number of large files,

Use php to obtain the number of file lines. The answer provided on the internet is usually to read files at a time, which is not applicable to large files. Generally, large files use the while clause to perform row-by-row statistics cyclically, which is too slow.

The fastest way is to perform multi-row statistics, read N bytes each time, and then count the number of rows, which is much more efficient than row-by-row statistics.

Test results: the file size is 3.14 GB.

1st times: line: 13214810, time: 56.2779 s;
2nd times: line: 13214810, time: 49.6678 s;

/** Calculate the number of lines of files efficiently * @ author axiang */function count_line ($ file) {$ fp = fopen ($ file, "r"); $ I = 0; while (! Feof ($ fp) {// Read 2 M if ($ data = fread ($ fp, 1024*1024*2) each time )) {// calculate the number of rows read $ num = substr_count ($ data, "\ n"); $ I + = $ num ;}} fclose ($ fp ); return $ I ;}

The above is all the content of this article. I hope you will like it.

Used to obtain the number of objects in php. The answer provided on the internet is usually one-time reading like file, which is not applicable to large files. Usually large files are large...

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.