Re: How to jump to line number in large file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, 2008-04-05 at 16:19 -0700, Jim Lucas wrote:
> Robert Cummings wrote:
> >>
> >> - Taking away the unnecessary fseek() made the script execute in 63 seconds
> >> - Using a buffer system, (reading in 1Mb of the text file at a time and then 
> >> looping through the string in memory) made the script execute in 36 seconds. 
> >> Huge improvement, but...
> >> - Porting the code to C++, doing a shell_exec and reading the results back 
> >> in to PHP, took less than 2 seconds.
> >>
> >> As fgetc() etc are all effectively C wrappers I was quite surprised at the 
> >> speed increase....
> > 
> > It really depends on how you write your code... I ran the following
> > script on a 150 meg text log file containing 1905883 lines in 4 seconds
> > (note that it performs caching). Here's the script:
> > 
> > <?php
> > 
> > $path = $argv[1];
> > 
> > if( ($fPtr = fopen( $path, 'r' )) === false )
> > {
> >     echo "Couldn't open for reading: $path\n";
> >     exit();
> > }
> > 
> > $line = 1;
> > $lines[$line] = 0;
> > 
> > while( fgets( $fPtr ) !== false )
> > {
> >     $lines[++$line] = ftell( $fPtr );
> > }
> 
> couldn't you get away from incrementing a counter variable by simply 
> starting the array at index #1 ??
> 
> $lines[1] = 0;
> 
> while( fgets( $fPtr ) !== false )
> {
>      $lines[] = ftell( $fPtr );
> }
> 
> Wouldn't this make it faster?

Good catch. I drop about .2 seconds on my previous tests when I do that.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux