tags:

views:

131

answers:

4

Hello folks,

To move the "current byte" pointer in TStream class we can use property Position (e.g. MyStream.Position := 0) or using Seek method (e.g. MyStream.Seek(0, soFromBeginning). The question is, which one is more efficient (aka faster)? (I don't have the source, so I could not check it myself).

So far I always use Seek in positioning that said pointer.

+8  A: 

Apart from the function call overhead to the Position property setter, there is no difference as setting the Position property calls Seek with the given value and starting at the beginning.:

procedure TStream.SetPosition(const Pos: Int64);
begin
  Seek(Pos, soBeginning);
end;
Marjan Venema
Sigh, so actually there is no difference (beside the call overhead).. So I guess I'll stick with seek to save that call overhead.. :)
DonnVall
This implementation of SetPosition will always call the 64-Bit version of seek, while a direct call to Seek might end up in the 32-Bit version. See my answer for the differences.
Uwe Raabe
+1  A: 

I dont think there is any big performance between them.

Stream.Seek(0, soFromBeginning)

This can take different parameters, so you can seek from the start, current position or end of stream.

Stream.Position

This gets or sets the absolute position of the stream.

kyndigs
+3  A: 

As TStream.Seek is an overloaded function handling 32-Bit or 64-Bit values, it depends on the current stream implementation which might be the better choice.

For instance TCustomMemoryStream implements the 32-Bit version of seek. When you set Position on that stream this will first call the 64-Bit version, which casts the value to a Longint while calling the 32-Bit version. (This will probably change with a 64-Bit version of Delphi!)

On the other hand a THandleStream implements the 64-Bit version of Seek. When you call Seek with a 32-Bit value you end up in a quite nasty mechanism calling the 64-Bit version.

My personal advice would be to set Position. At least it will be the better choice in the future.

Uwe Raabe
The problem is I still can not see the point using Position is more recommended than using Seek, since setter Position is in the end simply calling Seek.
DonnVall
Scratch my previous comment. I just took a peek to D7 vcl source and inspected the code completion hint on D2010, now I can see your point. Now I have to change my coding behavior...
DonnVall
A: 

Both are the same. You should use the option that is more legible.

Junior