Re: PHP Read Text File
- From: Jerry Stuckle <jstucklex@xxxxxxxxxxxxx>
- Date: Tue, 22 Apr 2008 18:26:36 -0400
Iván Sánchez Ortega wrote:
Jerry Stuckle wrote:
With your code it takes many more CPU cycles to accomplish the same
thing, during which time nothing else requiring CPU cycles can be
processed.
Pardon me?
Complexity of my algorithm (the same as GNU "tail") is O(n), where n is the
number of bytes that make up the desired lines at the end of the file.
Efficiency here depends on strrpos(), which has an complexity of O(n).
Complexity of your algorithm is O(m^2 * log(m)), where m is the total size
of the file. file() must check *every* character read to see if it's a line
break - that takes O(m). Then, you're doing count() - as PHP arrays are
hash tables (well, ordered maps), it is well known that transversing it
takes O(m*log(m)).
What is your basis to say that parsing the entire file is more CPU efficient
than parsing the last lines starting from the end? Because the way I see
it, O(n) << O(m^2*log(m)).
Cheers,
tail is a compiled program. It is much more efficient than an interpreted one.
And the only searching the program has to do is for the new line character. Even in an interpreted language, that can be optimized be quite a fast operation.
As opposed to multiple calls to seek and read the file, doing your own searching... Much more code to go through and much more cpu intensive.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.
- References:
- PHP Read Text File
- From: tlpell
- Re: PHP Read Text File
- From: tlpell
- Re: PHP Read Text File
- From: Jerry Stuckle
- Re: PHP Read Text File
- From: C. (http://symcbean.blogspot.com/)
- Re: PHP Read Text File
- From: Jerry Stuckle
- Re: PHP Read Text File
- From: Jerry Stuckle
- Re: PHP Read Text File
- From: Jerry Stuckle
- PHP Read Text File
- Prev by Date: Re: Printing email
- Next by Date: Re: Sessions not working in windows vista
- Previous by thread: Re: PHP Read Text File
- Next by thread: Re: PHP Read Text File
- Index(es):
Relevant Pages
|