Re: How to jump to line number in large file

From: Date: Sun, 06 Apr 2008 02:13:44 +0000
Subject: Re: How to jump to line number in large file
References: 1 2 3 4 5 6 7  Groups: php.general 
Request: Send a blank email to php-general+get-272577@lists.php.net to get a copy of this message
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

« previous php.general (#272577) next »