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