If you read byte for byte instead of in blocks and you are compiling in {$I+} state, every byte you read cause an IO check call! You *must* use {$I-} state for speed. That is the same as C defaults to.
If sysutils is included it gets worse, because then the IO checking is wrapped in exceptions and that makes the code even slower. If you want to compare with plain C all these things need to be considered. Once you level the playing field, there is really hardly speed difference. Make sure the buffer size in FPC and Pascal are the same.
C is usually very bare bone code and that can also be done in FPC.
This:
#include <stdio.h>
int main() {
FILE *file;
char ch; // char is byte!!
file
= fopen("filename.txt", "r"); // file must exist ch
= fgetc(file
);// checks eof while (ch != EOF) {
}
return 0;
}
Has the same speed as this:
program inpascal;
{$I-}
var
f:file of byte;
b:byte;
begin
Assign(f,'filename.txt');// file must exist
Reset(f);
Read(f,b);// checks eof
While not eof(f) do
read(f,b);
close(f);
end.
Even the generated assembler is similar... GNU C and FPC trunk on win64. -O1