Recent

Author Topic: Slow speed reading a binary file  (Read 3077 times)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11931
  • FPC developer.
Re: Slow speed reading a binary file
« Reply #15 on: June 25, 2024, 06:02:20 pm »
Hi - This is all very interesting. Just for fun I ran Thaddy's code (reply #6) with a counter added (just to give me some return). I used a 7.5MB text file. As before, the pascal version took about 32 seconds, while the c version was almost instantaneous (~5 sec). I am comparing older-style pascal on purpose, as I am simply interested as to why it is slower, as I see no reason why it should be (as stated by others)...

The default buffer size of FPC is a TP compatible 128 bytes. Use SetTextbuf to allocate a large ones as mentioned elsewhere in the thread.

Laur

  • New Member
  • *
  • Posts: 35
Re: Slow speed reading a binary file
« Reply #16 on: June 25, 2024, 07:22:06 pm »
I hope this is the right area to ask this. I have noticed that reading binary files is very slow compared to other languages, at least using older pascal verbage. As an example this:

while not EOF(subject_file) do
    begin
      read(subject_file, b);
      TotalCount := TotalCount + 1;
    end;

is more than 10x slower than equivalent C language. I have wondered about this for years, and finally decided to ask for some input. Does anyone have an ideas about this?

Try use file mapping - something like this (i do not know fp but it should work - for any system):

p = createMapOfFile(filename);

and now the p i just a pointer to data stored on a file.

if p is char type then:
p[7] is a 8 byte of the file...

This method is the fastest possible for file reading, and in any op. system.

DMStevenson

  • Newbie
  • Posts: 5
Re: Slow speed reading a binary file
« Reply #17 on: June 25, 2024, 07:28:59 pm »
Thanks! After your suggestions and some reading I think I understand what's behind the slowness, and yes, increasing the buffer makes all the difference. Thank's everyone!

Thaddy

  • Hero Member
  • *****
  • Posts: 16144
  • Censorship about opinions does not belong here.
Re: Slow speed reading a binary file
« Reply #18 on: June 25, 2024, 07:45:51 pm »
Hi - This is all very interesting. Just for fun I ran Thaddy's code (reply #6) with a counter added (just to give me some return). I used a 7.5MB text file. As before, the pascal version took about 32 seconds, while the c version was almost instantaneous (~5 sec). I am comparing older-style pascal on purpose, as I am simply interested as to why it is slower, as I see no reason why it should be (as stated by others)...
that can only be caused by buffering and your original code HAS no buffering. When used on par, the compilers are on par.
btw I don't believe you tested with -O1 or you forgot about {$I-} or you forgot about not including sysutils. (because I did test it)
« Last Edit: June 25, 2024, 07:50:29 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5750
  • Compiler Developer
Re: Slow speed reading a binary file
« Reply #19 on: June 25, 2024, 08:15:06 pm »
This method is the fastest possible for file reading, and in any op. system.

Not in every operating system. There can be operating systems that don't support that concept at all either because they don't support virtual memory (e.g. MS-DOS) or because a concept like that is an unnecessary complication.

DMStevenson

  • Newbie
  • Posts: 5
Re: Slow speed reading a binary file
« Reply #20 on: June 25, 2024, 08:47:15 pm »
Hi - Yes, Thaddy, you are right. When I 'fixed' the buffering the execution between pascal and C were identical. Thanks very much for the help!!!

 

TinyPortal © 2005-2018