Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6062627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:04:25+00:00 2026-05-23T09:04:25+00:00

In a function that reads data (data meaning exclusively strings) from disk, which should

  • 0

In a function that reads data (data meaning exclusively strings) from disk, which should I prefer? Which is better?

A) DiskStream.Read(Pointer(s)^, Count)
or
B) DiskStream.Read(s[1], Count)

Note:
I know both are having the same result.
I know that I have to SetLength of S before calling Read.


UPDATE

S is AnsiString.

Here is the full function:

{ Reads a bunch of chars from the file. Why ‘ReadChars’ and not ‘ReadString’? This function reads C++ strings (the length of the string was not written to disk also). So, i have to give the number of chars to read as parameter. }

function TMyStream.ReadChars(out s: AnsiString; CONST Count: Longint): Boolean; 
begin
 SetLength(s, Count);
 Result:= Read(s[1], Count)= Count;
end;

Speed test

In my speed test the first approach was a tiny bit faster than the second one. I used a 400MB file from which I read strings about 200000 times. The process was set to High priority.

The best read time ever was:
1.35 for variant B and 1.37 for variant A.
Average:
On average, B was scoring also 20ms better than A.

The test was repeated 15 times for each variant.

The difference is really small. It could fall into the measuring error range.
Probably it will be significant if I read strings more often and from a bigger file.
But for the moment let’s say that both lines of code are performing the same.

ANSWER
Variant A – might be a tiny tiny bit faster
Variant B – is (obviously) much more easier to read and it is more Delphi-ish. My preferred.

Note:
I have seen Embarcadero using variant A in TStreamReadBuffer example, but with a TBytes instead of String.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:04:25+00:00Added an answer on May 23, 2026 at 9:04 am

    Be aware that when running

    1. DiskStream.Read(Pointer(s)^, Count)
    2. DiskStream.Read(s[1], Count)
    

    The 1. version will be faster.

    But you must be sure that the s variable is explicitly local, or you have called yourself UniqueString(s) before the loop.

    Since pointer(s)^ won’t call UniqueString?() low-level hidden RTL call, it will be faster than s[1], but you may override some existing data if the s string variable is shared between the current context and other context (e.g. if the last content of s was retrieved from a function from a property value, or s is sent as parameter to another method).

    In fact the fastest correct way of coding this reading an AnsiString from content is:

      s := '';
      SetLength(s,Count);
      DiskStream.Read(pointer(s)^,Count);
    

    or

      SetString(s,nil,Count);
      DiskStream.Read(pointer(s)^,Count);
    

    The 2nd version being equal to the 1st, but with one line less.

    Setting s to ” will call FreeMem()+AllocMem() instead of ReallocMem() in SetLength(), so will avoid a call to move(), and will be therefore a bit faster.

    In fact, the UniqueString?() RTL call generated by s[1] will be very fast, since you have already called SetLength() before calling it: therefore, s is already unique, and UniqueString?() RTL call will return almost immediately. After profiling, there is not much speed difference between the two versions: almost all time is spend in string allocation and content moving from disk. Perhaps s[1] is found to be more “pascalish”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dll which includes a function called ReadPort that reads data from
Assume I have a function that reads data from a MySQL table, manipulates it
I make several calls to a function that reads data from an input file.
Id like to be able to write a function that reads an external news
I read that you should define your JavaScript functions in the <head> tag, but
I'm working on function, that takes a select from html and replaces it with
I am writing a utility which accepts either a filename, or reads from stdin.
.NET structs are value types, meaning that if function A creates a struct and
Anyone got a ready made function that will take an XML string and return
Jquery has a great language construct that looks like this: $(document).ready(function() { $(a).click(function() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.