a fast line counter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jf

    #1

    a fast line counter

    Hi,

    I've a big file, and I want to count the number of lines.

    I tried this:
    $i=0;
    $fp =fopen('file',' r');
    while ( ! feof ($fp) ) {
    $ligne = fgets($fp,8096) ;
    $i++;
    }

    and this:
    $i = exec("wc -l file");

    The first solution is slower, but the second isn't cross system.

    How can I do fast and clean ?

    Thanks.
  • Michael Fesser

    #2
    Re: a fast line counter

    .oO(jf)
    [color=blue]
    >I've a big file, and I want to count the number of lines.
    >
    >I tried this:
    >$i=0;
    >$fp =fopen('file',' r');
    >while ( ! feof ($fp) ) {
    > $ligne = fgets($fp,8096) ;
    > $i++;
    >}
    >
    >and this:
    >$i = exec("wc -l file");[/color]

    There's a third:

    $i = count(file('fil e')); // without error checking
    [color=blue]
    >The first solution is slower, but the second isn't cross system.
    >
    >How can I do fast and clean ?[/color]

    Put it all into a function, that first tries to determine the running OS
    (check the predefined constant PHP_OS). If you're on Win there's not
    much of a choice - go the safe way.

    If you're on Linux or something else that might have the wc command, try
    to execute it, but watch the returned error code of exec(). If the
    command doesn't exist or the execution fails use the safe method.

    Micha

    Comment

    • Jamie Davison

      #3
      Re: a fast line counter




      <?php

      function get_numlines($f ile)
      {
        $lines = count(file($fil e));
      return $lines
      }

      $myfile = "/path/to/your/dir/somefile.txt";
      echo get_numlines($m yfile);
      ?>





      On 9/24/04 6:26 PM, in article 20040925002611. 764089e7@wxp.af, "jf"
      <no@no.spam> wrote:
      [color=blue]
      > Hi,
      >
      > I've a big file, and I want to count the number of lines.
      >
      > I tried this:
      > $i=0;
      > $fp =fopen('file',' r');
      > while ( ! feof ($fp) ) {
      > $ligne = fgets($fp,8096) ;
      > $i++;
      > }
      >
      > and this:
      > $i = exec("wc -l file");
      >
      > The first solution is slower, but the second isn't cross system.
      >
      > How can I do fast and clean ?
      >
      > Thanks.[/color]

      Comment

      • jf

        #4
        Re: a fast line counter

        Bzzzzzzz...
        Wrong answer !
        :)

        It's a big file, with file($file) php load all the file in memory: "Allowedmem ory size exhausted". I don't want to extend the allowed memory.

        Regards.
        JF

        On Fri, 24 Sep 2004 22:57:44 GMT
        Jamie Davison <jdavison_usa@y ahoo.com> wrote:
        [color=blue]
        >
        >
        >
        > <?php
        >
        > function get_numlines($f ile)
        > {
        >   $lines = count(file($fil e));
        > return $lines
        > }
        >
        > $myfile = "/path/to/your/dir/somefile.txt";
        > echo get_numlines($m yfile);
        > ?>
        >
        >
        >
        >
        >
        > On 9/24/04 6:26 PM, in article 20040925002611. 764089e7@wxp.af, "jf"
        > <no@no.spam> wrote:
        > [color=green]
        > > Hi,
        > >
        > > I've a big file, and I want to count the number of lines.
        > >
        > > I tried this:
        > > $i=0;
        > > $fp =fopen('file',' r');
        > > while ( ! feof ($fp) ) {
        > > $ligne = fgets($fp,8096) ;
        > > $i++;
        > > }
        > >
        > > and this:
        > > $i = exec("wc -l file");
        > >
        > > The first solution is slower, but the second isn't cross system.
        > >
        > > How can I do fast and clean ?
        > >
        > > Thanks.[/color]
        > [/color]

        Comment

        • Chung Leong

          #5
          Re: a fast line counter

          "jf" <no@no.spam> wrote in message news:2004092500 2611.764089e7@w xp.af...[color=blue]
          > Hi,
          >
          > I've a big file, and I want to count the number of lines.
          >
          > I tried this:
          > $i=0;
          > $fp =fopen('file',' r');
          > while ( ! feof ($fp) ) {
          > $ligne = fgets($fp,8096) ;
          > $i++;
          > }
          >[/color]

          Instead of fgets(), grab the content in larger chunks with fread(), then
          count the occurrence of \n with substr_count().

          $i=0;
          $fp =fopen('file',' r');
          while ($chunk = fread($p, 1024000)) {
          $i += substr_count($c hunk, "\n");
          }



          Comment

          • Simon Stienen

            #6
            Re: a fast line counter

            Chung Leong <chernyshevsky@ hotmail.com> wrote:[color=blue]
            > "jf" <no@no.spam> wrote in message news:2004092500 2611.764089e7@w xp.af...[color=green]
            >> Hi,
            >>
            >> I've a big file, and I want to count the number of lines.
            >>
            >> I tried this:
            >> $i=0;
            >> $fp =fopen('file',' r');
            >> while ( ! feof ($fp) ) {
            >> $ligne = fgets($fp,8096) ;
            >> $i++;
            >> }
            >>[/color]
            >
            > Instead of fgets(), grab the content in larger chunks with fread(), then
            > count the occurrence of \n with substr_count().
            >
            > $i=0;
            > $fp =fopen('file',' r');
            > while ($chunk = fread($p, 1024000)) {
            > $i += substr_count($c hunk, "\n");
            > }[/color]

            Bad luck if it's a macintosh file :o)
            Maybe he should count \r in a different variable, too, and take the maximum
            (of \n and \r) afterwards.
            --
            Simon Stienen <http://dangerouscat.ne t> <http://slashlife.de>
            »What you do in this world is a matter of no consequence,
            The question is, what can you make people believe that you have done.«
            -- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle

            Comment

            Working...