Why is my "cat" function with system calls slower compared to Linux's "cat"?

Ah, based on your edit you were being bitten by the readahead buffer. You cannot test two programs that read files side by side by running them once. The first always be slower since the file is on disk, once the file is in memory the second will run faster, you must either create new data for each or run one and then run both so they both get the benefit of the readahead buffer.


Perhaps you compiled without optimization (or without as high an optimization setting)?

Also, your code will call sysWriteBuffer once with readBytes equal to zero -- maybe that (partially) explains it?

You might also inline sysWriteBuffer (either via a compiler switch or by hand).

"inlining" means to copy the body of a function to its call site in order to remove the overhead of calling a function. Sometimes compilers do this automatically (I think -O3 enables this optimization in gcc). You can also use the inline keyword in gcc to tell the compiler to inline a function. If you do this, your declaration will look like this:

static inline int sysWriteBuffer(int fdout, char *buffer, ssize_t readBytes) {
....

Research mmap(2).

You will be throwing aways niceties of ftell/fread, but it will skip a layer of indirection if read throughput is really important.