Slow tfilestream.EndOfData
-
- Registered: Oct 30, 2008
- Last visit: Mar 18, 2009
- Posts: 10
Hi
I have found the following code to be very slow (this also is the principle used in tstringlist.loadfromfile) :
Code
var
f : tfilestream;
....
while not f.endofdata do
begin
s := f.readln;
.....
end;
I have made some experiments and found the problem to be the way EodOfData works.
Whenever EndOfData is called, it tests weather current position is >= size, only problem with that is that size is a property hiding a seek to end of file and then back to current position.
Cut fron ustream :
Code
Function TStream.GetSize:LongInt;
Var OldPos:LongInt;
Begin
OldPos:=GetPosition;
Result:=Seek(0,Seek_End);
SetPosition(OldPos);
End;
Function TStream.EndOfData: Boolean;
Begin
Result := (Position >= Size);
End;
A solution to this could be to make tstream.GetSize virtual, and add the following to tfilestream :
interface :
Code
TFileStream = class(tstream)
Private
...
Function GetSize:LongInt; Override;
Implementation :
Code
Function TFileStream.GetSize:LongInt;
begin
result := filesize(PStreamFile);
end;
In a test, the original GetSize resulted in tstringlist.loadfromfile using several minutes reading a 600 kb text file, the corrected one used seconds reading the same file !
Kim
- Moderated by:
- Admins-Forum
Users on-line
- 0 users
This list is based on users active over the last 30 minutes.