Home >Backend Development >PHP Tutorial >How Can You Efficiently Count Lines in Gigabytes of Text Data?

How Can You Efficiently Count Lines in Gigabytes of Text Data?

Linda Hamilton
Linda HamiltonOriginal
2024-11-01 14:43:02313browse

How Can You Efficiently Count Lines in Gigabytes of Text Data?

Optimizing Line Counting in Large Text Files (Multi-GB)

For text files exceeding 200 MB, counting lines using count(file($path)) can encounter memory limitations. Here's a more efficient solution:

<code class="php">$file = "largefile.txt";
$linecount = 0;

$handle = fopen($file, "r");

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

fclose($handle);

echo $linecount;</code>

This approach loads a single line at a time into memory, avoiding the need to store the entire file in memory.

If your files may contain extremely long lines, you can use this alternative method to count line breaks instead:

<code class="php">$linecount = 0;

$handle = fopen($file, "r");

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

fclose($handle);</code>

By chunking the file and counting line breaks, you can mitigate memory issues even with exceptionally long lines.

The above is the detailed content of How Can You Efficiently Count Lines in Gigabytes of Text Data?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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