Re: [PHP] How to jump to line number in large file
Thanks for the heads up on fgetc() incrementing by one. I hadn't actually tested that code yet, I was using the original fseek($handle,$pos). strpos would be ideal but it needs to work on a string and not a file - I don't want to load a 100Mb file into memory if I don't have to. Perhaps I should test how quick the fgets() and ftell() method is because at least it loads in one line at a time. Does anybody know any other ways to go about the problem? Many thanks, Steve Greg Bowser [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] fseek($handle, 1, SEEK_CUR); // or fseek($handle, $pos) $t = fgetc($handle); This probably won't help you, but given a quick glance, it looks like you're incrementing the file pointer by 2 positions on each iteration of your while loop. The fgetc() function returns the character at the current position and increments the file pointer by 1. I haven't tried this, but perhaps using strpos would be faster? Use strpos to seek to find the first \n, then use the offset parameter to seek to the second, and so on, until strpos() returns false. --GREG -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to jump to line number in large file
Thanks for the heads up on fgetc() incrementing by one. I hadn't actually tested that code yet, I was using the original fseek($handle,$pos). strpos would be ideal but it needs to work on a string and not a file - I don't want to load a 100Mb file into memory if I don't have to. Perhaps I should test how quick the fgets() and ftell() method is because at least it loads in one line at a time. Does anybody know any other ways to go about the problem? Haven't read the rest of the thread, and so going by the subject alone, fgets() finishes when it encounters a newline, so you can use this wondrous fact to seek to a specific line: ?php $fp = fopen('filename', 'r'); $num = 18; // Desired line number for ($i=0; $i$num; $i++) $line = fgets($fp); echo $line; ? It works because fgets() stops when it encounters a newline (\n). So it's just a case of counting the calls to fgets(). -- Richard Heyes Employ me: http://www.phpguru.org/cv -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PostTrack Updates
On Apr 5, 2008, at 1:48 AM, Robert Cummings wrote: On Fri, 2008-04-04 at 22:39 -0700, Jim Lucas wrote: Robert Cummings wrote: ?php echo On Fri, 2008-04-04 at 17:23 -0400, Daniel Brown wrote: \n ? ?php echo Some changes take effect with the PostTrack metrics \n ? ?php echo system with this week (will show up in next week's \n ? ?php echo report). There's one bug fix and a new feature added, \n ? ?php echo in which some of you may be really interested.\n ? ?php echo \n ? ?php echo CHANGELOG\n ? ?php echo \n ? ?php echo Fixed a bug that would (seemingly random) \n ? ?php echo split a user's post recording over multiple entries. \n ? ?php echo (See this week's report: Zoltan Nemeth for example) \n ? ?php echo Added a new CodeCount feature. Explained \n ? ?php echo below.\n ? ?php echo \n ? ?php echo The CodeCount feature will, from now on, track \n ? ?php echo the amount of [pseudo]code everyone posts to the\n ? ?php echo list. However, for a variety of reasons (including \n ? ?php echo enforcing Good Coding Practices[tm], there will be \n ? ?php echo some rules:\n ? ?php echo \n ? ?php echo * short_open_tags code will not be counted. \n ? ?php echoIt must begin with ?php, not ?, and\n ? ?php echo?=\$variable;? things won't count.\n ? ?php echo * All code must be properly closed as well as \n ? ?php echoopened. Thus, ?php must be followed by ?.\n ? ?php echo * You can include multiple blocks of code, \n ? ?php echoand all will be tallied. So:\n ? ?php echo ?php\n ? ?php echo // Block one\n ? ?php echo ?\n ? ?php echo and \n ? ?php echo ?php\n ? ?php echo // Block two\n ? ?php echo ?\n ? ?php echo will both be counted.\n ? ?php echo \n ? ?php echo Some notes that should be obvious:\n ? ?php echo * HTML won't be counted unless included in an \n ? ?php echoecho/print construct or a HEREDOC/NOWDOC \n ? ?php echo(when used). Only the code between the\n ? ?php echo?php and ? tags will be counted.\n ? ?php echo * The CodeCount procedure *does* count\n ? ?php echocomments as code. This can be changed if \n ? ?php echothere's enough desire.\n ? ?php echo * CodeCount *will not* differentiate between \n ? ?php echo\Good\ and \Bad\ code. If you forget a \n ? ?php echosemicolon, it'll still count.\n ? ?php echo \n ? ?php echo --\n ? ?php echo /Daniel P. Brown\n ? ?php echo Ask me about:\n ? ?php echo Dedicated servers starting @ \$59.99/mo., VPS\n ? ?php echo starting @ \$19.99/mo., and shared hosting starting @ \n ? ?php echo \$2.50/mo. Unmanaged, managed, and fully-managed! \n ? I don't know about anyone else, but I am absolutely stoked about this development! Cheers, Rob. wait you forgot the semi-colon... ah, that's right, he said grammar mistakes were not counted... Perfectly valid to omit the last semi-colon when closing a PHP block. ?PHP echo 'So does the new code count all quoted code as well? :)';? ?PHP echo 'If so... Then we are going to have a ton of code to start with!';? :) ;) 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] everything as classes
On Thursday 03 April 2008, robert wrote: Along the lines of a previous post How to get a code review, I am curious if it is overkill to create everything in classes. For example, a movie website where there is a class for the movie datatype, class for getting/adding/deleting/updating movie's data to the database, and class for displaying the data. (Of course there are classes for general functionality like sql database connection etc.) For listing movies alone there are at least 4 different inheritance classes for chronological, alphabetic, category and keyword. Anyway this is how I coded something similar but for Flash/Actionscript site but I'm not sure about a PHP site. I appreciate your comments. - robert Procedural code is a hack saw. OOP is a table saw. Sometimes, all you need is a hack saw. It's simple, straightforward, easy to learn how to use, and you can pick it up and use it in various ways and places. It's harder to get wrong, and when you do you can usually just turn your hand at a weird angle for a bit and make it work. Other times, you really need a table saw. It takes longer to build and setup and can be more difficult to understand if you're not used to it, but if done properly will slice and dice wood in ways you couldn't even think of with a hack saw. Of course, if you build it wrong then you're now out a very expensive table. Use whichever makes sense for your use case. In a language like PHP that has strong support for both, both the everything is an object and never use objects viewpoints are, well, ignorant. :-) -- Larry Garfield AIM: LOLG42 [EMAIL PROTECTED] ICQ: 6817012 If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it. -- Thomas Jefferson -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PostTrack Updates
On Apr 5, 2008, at 6:23 AM, Jason Pruim [EMAIL PROTECTED] wrote: On Apr 5, 2008, at 1:48 AM, Robert Cummings wrote: On Fri, 2008-04-04 at 22:39 -0700, Jim Lucas wrote: Robert Cummings wrote: ?php echo On Fri, 2008-04-04 at 17:23 -0400, Daniel Brown wrote: \n ? ?php echo Some changes take effect with the PostTrack metrics \n ? ?php echo system with this week (will show up in next week's \n ? ?php echo report). There's one bug fix and a new feature added, \n ? ?php echo in which some of you may be really interested.\n ? ?php echo \n ? ?php echo CHANGELOG\n ? ?php echo \n ? ?php echo Fixed a bug that would (seemingly random) \n ? ?php echo split a user's post recording over multiple entries. \n ? ?php echo (See this week's report: Zoltan Nemeth for example) \n ? ?php echo Added a new CodeCount feature. Explained \n ? ?php echo below.\n ? ?php echo \n ? ?php echo The CodeCount feature will, from now on, track \n ? ?php echo the amount of [pseudo]code everyone posts to the \n ? ?php echo list. However, for a variety of reasons (including \n ? ?php echo enforcing Good Coding Practices[tm], there will be \n ? ?php echo some rules:\n ? ?php echo \n ? ?php echo * short_open_tags code will not be counted. \n ? ?php echoIt must begin with ?php, not ?, and\n ? ?php echo?=\$variable;? things won't count.\n ? ?php echo * All code must be properly closed as well as \n ? ?php echoopened. Thus, ?php must be followed by ?.\n ? ?php echo * You can include multiple blocks of code, \n ? ?php echoand all will be tallied. So:\n ? ?php echo ?php\n ? ?php echo // Block one\n ? ?php echo ?\n ? ?php echo and \n ? ?php echo ?php\n ? ?php echo // Block two\n ? ?php echo ?\n ? ?php echo will both be counted.\n ? ?php echo \n ? ?php echo Some notes that should be obvious:\n ? ?php echo * HTML won't be counted unless included in an \n ? ?php echoecho/print construct or a HEREDOC/NOWDOC \n ? ?php echo(when used). Only the code between the \n ? ?php echo?php and ? tags will be counted.\n ? ?php echo * The CodeCount procedure *does* count\n ? ?php echocomments as code. This can be changed if \n ? ?php echothere's enough desire.\n ? ?php echo * CodeCount *will not* differentiate between \n ? ?php echo\Good\ and \Bad\ code. If you forget a \n ? ?php echosemicolon, it'll still count.\n ? ?php echo \n ? ?php echo --\n ? ?php echo /Daniel P. Brown\n ? ?php echo Ask me about:\n ? ?php echo Dedicated servers starting @ \$59.99/mo., VPS\n ? ?php echo starting @ \$19.99/mo., and shared hosting starting @ \n ? ?php echo \$2.50/mo. Unmanaged, managed, and fully-managed! \n ? I don't know about anyone else, but I am absolutely stoked about this development! Cheers, Rob. wait you forgot the semi-colon... ah, that's right, he said grammar mistakes were not counted... Perfectly valid to omit the last semi-colon when closing a PHP block. ?PHP echo 'So does the new code count all quoted code as well? :)';? ?PHP echo 'If so... Then we are going to have a ton of code to start with!';? :) ;) 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Does it count if I quote it! :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PostTrack Updates
On Sat, 2008-04-05 at 09:23 -0400, Jason Pruim wrote: On Apr 5, 2008, at 1:48 AM, Robert Cummings wrote: On Fri, 2008-04-04 at 22:39 -0700, Jim Lucas wrote: Robert Cummings wrote: ?php echo On Fri, 2008-04-04 at 17:23 -0400, Daniel Brown wrote: \n ? ?php echo Some changes take effect with the PostTrack metrics \n ? ?php echo system with this week (will show up in next week's \n ? ?php echo report). There's one bug fix and a new feature added, \n ? ?php echo in which some of you may be really interested.\n ? ?php echo \n ? ?php echo CHANGELOG\n ? ?php echo \n ? ?php echo Fixed a bug that would (seemingly random) \n ? ?php echo split a user's post recording over multiple entries. \n ? ?php echo (See this week's report: Zoltan Nemeth for example) \n ? ?php echo Added a new CodeCount feature. Explained \n ? ?php echo below.\n ? ?php echo \n ? ?php echo The CodeCount feature will, from now on, track \n ? ?php echo the amount of [pseudo]code everyone posts to the\n ? ?php echo list. However, for a variety of reasons (including \n ? ?php echo enforcing Good Coding Practices[tm], there will be \n ? ?php echo some rules:\n ? ?php echo \n ? ?php echo * short_open_tags code will not be counted. \n ? ?php echoIt must begin with ?php, not ?, and\n ? ?php echo?=\$variable;? things won't count.\n ? ?php echo * All code must be properly closed as well as \n ? ?php echoopened. Thus, ?php must be followed by ?.\n ? ?php echo * You can include multiple blocks of code, \n ? ?php echoand all will be tallied. So:\n ? ?php echo ?php\n ? ?php echo // Block one\n ? ?php echo ?\n ? ?php echo and \n ? ?php echo ?php\n ? ?php echo // Block two\n ? ?php echo ?\n ? ?php echo will both be counted.\n ? ?php echo \n ? ?php echo Some notes that should be obvious:\n ? ?php echo * HTML won't be counted unless included in an \n ? ?php echoecho/print construct or a HEREDOC/NOWDOC \n ? ?php echo(when used). Only the code between the\n ? ?php echo?php and ? tags will be counted.\n ? ?php echo * The CodeCount procedure *does* count\n ? ?php echocomments as code. This can be changed if \n ? ?php echothere's enough desire.\n ? ?php echo * CodeCount *will not* differentiate between \n ? ?php echo\Good\ and \Bad\ code. If you forget a \n ? ?php echosemicolon, it'll still count.\n ? ?php echo \n ? ?php echo --\n ? ?php echo /Daniel P. Brown\n ? ?php echo Ask me about:\n ? ?php echo Dedicated servers starting @ \$59.99/mo., VPS\n ? ?php echo starting @ \$19.99/mo., and shared hosting starting @ \n ? ?php echo \$2.50/mo. Unmanaged, managed, and fully-managed! \n ? I don't know about anyone else, but I am absolutely stoked about this development! Cheers, Rob. wait you forgot the semi-colon... ah, that's right, he said grammar mistakes were not counted... Perfectly valid to omit the last semi-colon when closing a PHP block. ?PHP echo 'So does the new code count all quoted code as well? :)';? ?PHP echo 'If so... Then we are going to have a ton of code to start with!';? I don't know, but that's a very good question. 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
Re: [PHP] PostTrack Updates
On Sat, 2008-04-05 at 07:21 -0700, Casey wrote: On Apr 5, 2008, at 6:23 AM, Jason Pruim [EMAIL PROTECTED] wrote: On Apr 5, 2008, at 1:48 AM, Robert Cummings wrote: On Fri, 2008-04-04 at 22:39 -0700, Jim Lucas wrote: Robert Cummings wrote: ?php echo On Fri, 2008-04-04 at 17:23 -0400, Daniel Brown wrote: \n ? ?php echo Some changes take effect with the PostTrack metrics \n ? ?php echo system with this week (will show up in next week's \n ? ?php echo report). There's one bug fix and a new feature added, \n ? ?php echo in which some of you may be really interested.\n ? ?php echo \n ? ?php echo CHANGELOG\n ? ?php echo \n ? ?php echo Fixed a bug that would (seemingly random) \n ? ?php echo split a user's post recording over multiple entries. \n ? ?php echo (See this week's report: Zoltan Nemeth for example) \n ? ?php echo Added a new CodeCount feature. Explained \n ? ?php echo below.\n ? ?php echo \n ? ?php echo The CodeCount feature will, from now on, track \n ? ?php echo the amount of [pseudo]code everyone posts to the \n ? ?php echo list. However, for a variety of reasons (including \n ? ?php echo enforcing Good Coding Practices[tm], there will be \n ? ?php echo some rules:\n ? ?php echo \n ? ?php echo * short_open_tags code will not be counted. \n ? ?php echoIt must begin with ?php, not ?, and\n ? ?php echo?=\$variable;? things won't count.\n ? ?php echo * All code must be properly closed as well as \n ? ?php echoopened. Thus, ?php must be followed by ?.\n ? ?php echo * You can include multiple blocks of code, \n ? ?php echoand all will be tallied. So:\n ? ?php echo ?php\n ? ?php echo // Block one\n ? ?php echo ?\n ? ?php echo and \n ? ?php echo ?php\n ? ?php echo // Block two\n ? ?php echo ?\n ? ?php echo will both be counted.\n ? ?php echo \n ? ?php echo Some notes that should be obvious:\n ? ?php echo * HTML won't be counted unless included in an \n ? ?php echoecho/print construct or a HEREDOC/NOWDOC \n ? ?php echo(when used). Only the code between the \n ? ?php echo?php and ? tags will be counted.\n ? ?php echo * The CodeCount procedure *does* count\n ? ?php echocomments as code. This can be changed if \n ? ?php echothere's enough desire.\n ? ?php echo * CodeCount *will not* differentiate between \n ? ?php echo\Good\ and \Bad\ code. If you forget a \n ? ?php echosemicolon, it'll still count.\n ? ?php echo \n ? ?php echo --\n ? ?php echo /Daniel P. Brown\n ? ?php echo Ask me about:\n ? ?php echo Dedicated servers starting @ \$59.99/mo., VPS\n ? ?php echo starting @ \$19.99/mo., and shared hosting starting @ \n ? ?php echo \$2.50/mo. Unmanaged, managed, and fully-managed! \n ? I don't know about anyone else, but I am absolutely stoked about this development! Cheers, Rob. wait you forgot the semi-colon... ah, that's right, he said grammar mistakes were not counted... Perfectly valid to omit the last semi-colon when closing a PHP block. ?PHP echo 'So does the new code count all quoted code as well? :)';? ?PHP echo 'If so... Then we are going to have a ton of code to start with!';? Does it count if I quote it! :) You mean a quote of a quote of a quote? Didn't anyone teach you to trim my posts? ;) 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
Re: [PHP] PostTrack Updates
Top-posting because I don't care. I knew it would cause a lot of this up front, but eventually it could be potentially useful, once the monkeys realize that throwing poop isn't as much fun as it looked like on television. ;-P By the way, if you guys have ideas for improvement, feel free to modify the code. I have to repackage it with the new version, but I'll put it up soon. This concludes our top-posting day. Happy Saturday, all. On Sat, Apr 5, 2008 at 10:30 AM, Robert Cummings [EMAIL PROTECTED] wrote: On Sat, 2008-04-05 at 07:21 -0700, Casey wrote: On Apr 5, 2008, at 6:23 AM, Jason Pruim [EMAIL PROTECTED] wrote: On Apr 5, 2008, at 1:48 AM, Robert Cummings wrote: On Fri, 2008-04-04 at 22:39 -0700, Jim Lucas wrote: Robert Cummings wrote: ?php echo On Fri, 2008-04-04 at 17:23 -0400, Daniel Brown wrote: \n ? ?php echo Some changes take effect with the PostTrack metrics \n ? ?php echo system with this week (will show up in next week's \n ? ?php echo report). There's one bug fix and a new feature added, \n ? ?php echo in which some of you may be really interested.\n ? ?php echo \n ? ?php echo CHANGELOG\n ? ?php echo \n ? ?php echo Fixed a bug that would (seemingly random) \n ? ?php echo split a user's post recording over multiple entries. \n ? ?php echo (See this week's report: Zoltan Nemeth for example) \n ? ?php echo Added a new CodeCount feature. Explained \n ? ?php echo below.\n ? ?php echo \n ? ?php echo The CodeCount feature will, from now on, track \n ? ?php echo the amount of [pseudo]code everyone posts to the \n ? ?php echo list. However, for a variety of reasons (including \n ? ?php echo enforcing Good Coding Practices[tm], there will be \n ? ?php echo some rules:\n ? ?php echo \n ? ?php echo * short_open_tags code will not be counted. \n ? ?php echoIt must begin with ?php, not ?, and\n ? ?php echo?=\$variable;? things won't count.\n ? ?php echo * All code must be properly closed as well as \n ? ?php echoopened. Thus, ?php must be followed by ?.\n ? ?php echo * You can include multiple blocks of code, \n ? ?php echoand all will be tallied. So:\n ? ?php echo ?php\n ? ?php echo // Block one\n ? ?php echo ?\n ? ?php echo and \n ? ?php echo ?php\n ? ?php echo // Block two\n ? ?php echo ?\n ? ?php echo will both be counted.\n ? ?php echo \n ? ?php echo Some notes that should be obvious:\n ? ?php echo * HTML won't be counted unless included in an \n ? ?php echoecho/print construct or a HEREDOC/NOWDOC \n ? ?php echo(when used). Only the code between the \n ? ?php echo?php and ? tags will be counted.\n ? ?php echo * The CodeCount procedure *does* count\n ? ?php echocomments as code. This can be changed if \n ? ?php echothere's enough desire.\n ? ?php echo * CodeCount *will not* differentiate between \n ? ?php echo\Good\ and \Bad\ code. If you forget a \n ? ?php echosemicolon, it'll still count.\n ? ?php echo \n ? ?php echo --\n ? ?php echo /Daniel P. Brown\n ? ?php echo Ask me about:\n ? ?php echo Dedicated servers starting @ \$59.99/mo., VPS\n ? ?php echo starting @ \$19.99/mo., and shared hosting starting @ \n ? ?php echo \$2.50/mo. Unmanaged, managed, and fully-managed! \n ? I don't know about anyone else, but I am absolutely stoked about this development! Cheers, Rob. wait you forgot the semi-colon... ah, that's right, he said grammar mistakes were not counted... Perfectly valid to omit the last semi-colon when closing a PHP block. ?PHP echo 'So does the new code count all quoted code as well? :)';? ?PHP echo 'If so... Then we are going to have a ton of code to start with!';? Does it count if I quote it! :) You mean a quote of a quote of a quote? Didn't anyone teach you to trim my posts? ;) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- /Daniel P. Brown Ask me about: Dedicated servers starting @ $59.99/mo., VPS starting @ $19.99/mo., and shared hosting starting @ $2.50/mo. Unmanaged, managed, and fully-managed! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to jump to line number in large file
Richard Heyes [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Thanks for the heads up on fgetc() incrementing by one. I hadn't actually tested that code yet, I was using the original fseek($handle,$pos). strpos would be ideal but it needs to work on a string and not a file - I don't want to load a 100Mb file into memory if I don't have to. Perhaps I should test how quick the fgets() and ftell() method is because at least it loads in one line at a time. Does anybody know any other ways to go about the problem? Haven't read the rest of the thread, and so going by the subject alone, fgets() finishes when it encounters a newline, so you can use this wondrous fact to seek to a specific line: ?php $fp = fopen('filename', 'r'); $num = 18; // Desired line number for ($i=0; $i$num; $i++) $line = fgets($fp); echo $line; ? It works because fgets() stops when it encounters a newline (\n). So it's just a case of counting the calls to fgets(). fgets() would work but as I'm constantly jumping around a 500,000 line file I thought it was better to maintain a cache of line number positions. As a final update to anybody following: - 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 Best wishes, Steve -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to jump to line number in large file
On Sat, 2008-04-05 at 19:09 +0100, Steve McGill wrote: Richard Heyes [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Thanks for the heads up on fgetc() incrementing by one. I hadn't actually tested that code yet, I was using the original fseek($handle,$pos). strpos would be ideal but it needs to work on a string and not a file - I don't want to load a 100Mb file into memory if I don't have to. Perhaps I should test how quick the fgets() and ftell() method is because at least it loads in one line at a time. Does anybody know any other ways to go about the problem? Haven't read the rest of the thread, and so going by the subject alone, fgets() finishes when it encounters a newline, so you can use this wondrous fact to seek to a specific line: ?php $fp = fopen('filename', 'r'); $num = 18; // Desired line number for ($i=0; $i$num; $i++) $line = fgets($fp); echo $line; ? It works because fgets() stops when it encounters a newline (\n). So it's just a case of counting the calls to fgets(). fgets() would work but as I'm constantly jumping around a 500,000 line file I thought it was better to maintain a cache of line number positions. As a final update to anybody following: - 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 ); } fclose( $fPtr ); ? Here's the run times on several iterations (Athlon 2400+): real0m4.065s user0m3.488s sys 0m0.464s real0m4.005s user0m3.464s sys 0m0.436s real0m5.816s user0m3.336s sys 0m0.536s real0m3.994s user0m3.384s sys 0m0.504s real0m4.069s user0m3.512s sys 0m0.444s real0m4.009s user0m3.344s sys 0m0.552s 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
Re: [PHP] How to jump to line number in large file
So the only variation on a theme that I didn't test is the one that performs the best by an order of magnitude... nice. Many thanks for your time everyone. Robert Cummings [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Sat, 2008-04-05 at 19:09 +0100, Steve McGill wrote: Richard Heyes [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Thanks for the heads up on fgetc() incrementing by one. I hadn't actually tested that code yet, I was using the original fseek($handle,$pos). strpos would be ideal but it needs to work on a string and not a file - I don't want to load a 100Mb file into memory if I don't have to. Perhaps I should test how quick the fgets() and ftell() method is because at least it loads in one line at a time. Does anybody know any other ways to go about the problem? Haven't read the rest of the thread, and so going by the subject alone, fgets() finishes when it encounters a newline, so you can use this wondrous fact to seek to a specific line: ?php $fp = fopen('filename', 'r'); $num = 18; // Desired line number for ($i=0; $i$num; $i++) $line = fgets($fp); echo $line; ? It works because fgets() stops when it encounters a newline (\n). So it's just a case of counting the calls to fgets(). fgets() would work but as I'm constantly jumping around a 500,000 line file I thought it was better to maintain a cache of line number positions. As a final update to anybody following: - 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 ); } fclose( $fPtr ); ? Here's the run times on several iterations (Athlon 2400+): real0m4.065s user0m3.488s sys 0m0.464s real0m4.005s user0m3.464s sys 0m0.436s real0m5.816s user0m3.336s sys 0m0.536s real0m3.994s user0m3.384s sys 0m0.504s real0m4.069s user0m3.512s sys 0m0.444s real0m4.009s user0m3.344s sys 0m0.552s 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
Re: [PHP] PostTrack Updates
On Sat, Apr 5, 2008 at 12:51 PM, Daniel Brown [EMAIL PROTECTED] wrote: Top-posting because I don't care. I knew it would cause a lot of this up front, but eventually it could be potentially useful, once the monkeys realize that throwing poop isn't as much fun as it looked like on television. ;-P By the way, if you guys have ideas for improvement, feel free to modify the code. I have to repackage it with the new version, but I'll put it up soon. ?php ${well dan, im not a fan of the restriction of html only being tallied if embedded in strings. one of the primary features of php is the 'templating' aspect} = 'eg.'; ? ?php $phpIsCool = 'yes we know'; ? p$phpIsCool/p ?php $shouldPhpTemplatingBeCounted = 'i think so'; ? p$shouldPhpTemplatingBeCounted/p ?php ${The next cool feature we need to think about is a rant counter ;)} ='nathan'; ?
Re: [PHP] How to jump to line number in large file
Robert Cummings wrote: On Sat, 2008-04-05 at 19:09 +0100, Steve McGill wrote: Richard Heyes [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Thanks for the heads up on fgetc() incrementing by one. I hadn't actually tested that code yet, I was using the original fseek($handle,$pos). strpos would be ideal but it needs to work on a string and not a file - I don't want to load a 100Mb file into memory if I don't have to. Perhaps I should test how quick the fgets() and ftell() method is because at least it loads in one line at a time. Does anybody know any other ways to go about the problem? Haven't read the rest of the thread, and so going by the subject alone, fgets() finishes when it encounters a newline, so you can use this wondrous fact to seek to a specific line: ?php $fp = fopen('filename', 'r'); $num = 18; // Desired line number for ($i=0; $i$num; $i++) $line = fgets($fp); echo $line; ? It works because fgets() stops when it encounters a newline (\n). So it's just a case of counting the calls to fgets(). fgets() would work but as I'm constantly jumping around a 500,000 line file I thought it was better to maintain a cache of line number positions. As a final update to anybody following: - 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? fclose( $fPtr ); ? Here's the run times on several iterations (Athlon 2400+): real0m4.065s user0m3.488s sys 0m0.464s real0m4.005s user0m3.464s sys 0m0.436s real0m5.816s user0m3.336s sys 0m0.536s real0m3.994s user0m3.384s sys 0m0.504s real0m4.069s user0m3.512s sys 0m0.444s real0m4.009s user0m3.344s sys 0m0.552s Cheers, Rob. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PostTrack Updates
?php $enough = 'enough'; ? On Sat, Apr 5, 2008 at 7:10 PM, Nathan Nobbe [EMAIL PROTECTED] wrote: On Sat, Apr 5, 2008 at 12:51 PM, Daniel Brown [EMAIL PROTECTED] wrote: Top-posting because I don't care. I knew it would cause a lot of this up front, but eventually it could be potentially useful, once the monkeys realize that throwing poop isn't as much fun as it looked like on television. ;-P By the way, if you guys have ideas for improvement, feel free to modify the code. I have to repackage it with the new version, but I'll put it up soon. ?php ${well dan, im not a fan of the restriction of html only being tallied if embedded in strings. one of the primary features of php is the 'templating' aspect} = 'eg.'; ? ?php $phpIsCool = 'yes we know'; ? p$phpIsCool/p ?php $shouldPhpTemplatingBeCounted = 'i think so'; ? p$shouldPhpTemplatingBeCounted/p ?php ${The next cool feature we need to think about is a rant counter ;)} ='nathan'; ? -- /Daniel P. Brown Ask me about: Dedicated servers starting @ $59.99/mo., VPS starting @ $19.99/mo., and shared hosting starting @ $2.50/mo. Unmanaged, managed, and fully-managed! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to jump to line number in large file
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
Re: [PHP] PostTrack Updates
On Sat, 2008-04-05 at 21:54 -0400, Daniel Brown wrote: ?php $enough = 'enough'; ? Hmmm... ?php !$bloodyLikely ? 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
Re: [PHP] APC FastCGI != upload progress ?
Hello, on 04/04/2008 03:16 AM steve said the following: FastCGI is *the* way to run PHP, but I think Apache is not the platform for it anymore. If need more than one server, Apache pre-forked model may limited. Otherwise it is just fine. Other than that, I think there are some features that only work with Apache SAPI. Is the COMMET implementation on the client side long polling (reconnect) or does it stream using script tags? What goes on in the server? Does it push the buffer of blank data that WebKit requires? Is it a PHP daemon process running as a simple HTTP server? Actually it is just an hidden iframe that loads an HTML page with small Javascript chunks that flush each COMET AJAX server response. This is a regular HTTP request performed to the same script that serves that form. The form AJAX plug-in can detect the AJAX request and respond adequately. So it works equally well in all browsers including Webkit. On Wed, Apr 2, 2008 at 12:09 AM, Manuel Lemos [EMAIL PROTECTED] wrote: Hello, on 03/30/2008 02:52 PM steve said the following: Hmmm... I am working on a PHP daemon for comet style connections... I'll keep that idea in mind. I guess that using Flash is best solution at the moment.. at least the only one I have working... I implement COMET connections with plain HTML with an hidden iframe. Actually I have been using that for a upload progress meter among other AJAX uses. Actually it is an AJAX plug-in of this forms class: http://www.phpclasses.org/formsgeneration Here is an example of a form upload progress done all in PHP and plain HTML: http://www.meta-language.net/forms-examples.html?example=test_upload_progress Here you can watch a tutorial video that explains the COMET AJAX implementation: http://www.phpclasses.org/browse/video/1/package/1/section/plugin-ajax.html -- Regards, Manuel Lemos PHP professionals looking for PHP jobs http://www.phpclasses.org/professionals/ PHP Classes - Free ready to use OOP components written in PHP http://www.phpclasses.org/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PostTrack Updates
?php $stfu = true; if($stfu == true) { stop_posting( ); }else{ post( ); } ? Original Message: - From: Robert Cummings [EMAIL PROTECTED] Date: Sat, 05 Apr 2008 22:14:44 -0400 To: [EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL PROTECTED], php-general@lists.php.net Subject: Re: [PHP] PostTrack Updates On Sat, 2008-04-05 at 21:54 -0400, Daniel Brown wrote: ?php $enough = 'enough'; ? Hmmm... ?php !$bloodyLikely ? 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 mail2web - Check your email from the web at http://link.mail2web.com/mail2web -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PostTrack Updates
On Sat, 2008-04-05 at 22:54 -0400, [EMAIL PROTECTED] wrote: ?php $stfu = true; if($stfu == true) { stop_posting( ); }else{ post( ); } ? Please don't top-post. It makes it hard to follow the discussion :B 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