开发者

TStream.Position compared to TStream.Seek

To move the "current byte" pointer in TStream class we can use propert开发者_如何学Cy 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.


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.


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;


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.


Just to be a reference for readers: Seek is deprecated in Delphi/Rad Studio XE4, so you should use TStream.Position instead of Seek.


As I mentioned in one of the comments, there are 3 overloaded methods of Seek. One of them is deprecated, the one were the second argument "Origin" is a Word instead of a TSeekOrigin.

TSeekOrigin = (soBeginning, soCurrent, soEnd);

So if everyone is using TSeekOrigin this problem should be solved.

Better would of course be just to set the position, like the accepted solution...


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜