How to Efficiently Count Lines in Large Text Files (200 MB )?

Susan Sarandon
Release: 2024-11-02 07:38:02
Original
322 people have browsed it

How to Efficiently Count Lines in Large Text Files (200 MB )?

Counting Lines in Large Text Files (200 MB ) without Memory Errors

Counting lines in large text files using file($path) can lead to memory exhaustion errors as the entire file is loaded into memory. To address this issue, a more efficient and memory-saving approach is required.

One method for efficiently counting lines is to use the fgets() function. By reading and processing lines one at a time, you can avoid loading the entire file into memory:

<code class="php">$file = "largefile.txt";
$linecount = 0;
$handle = fopen($file, "r");

while (!feof($handle)) {
  $line = fgets($handle);
  $linecount++;
}

fclose($handle);

echo $linecount;</code>
Copy after login

This code opens the file for reading, reads a single line at a time using fgets(), increments the line count, and terminates when the end of the file is reached.

Another approach, particularly useful for files with potentially very long lines, is to count end-of-line characters:

<code class="php">$file = "largefile.txt";
$linecount = 0;
$handle = fopen($file, "r");

while (!feof($handle)) {
  $line = fgets($handle, 4096);
  $linecount += substr_count($line, PHP_EOL);
}

fclose($handle);

echo $linecount;</code>
Copy after login

Here, substr_count() is used to count the number of line breaks in each chunk of data read.

Both these methods use less memory since they process lines incrementally rather than loading the entire file into memory.

The above is the detailed content of How to Efficiently Count Lines in Large Text Files (200 MB )?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!