Why do you care that much about performance?
1 gigabyte from /dev/urandom
can be piped into wc
in 1 minutes (and wc
is running 15% of the time, waiting for data on the rest) ! Just try time (head -1000000000c /dev/urandom|wc)
But the fastest way would be to use the read(2) syscall with a quite big buffer (e.g. 64Kbytes to 256Kbytes).
Of course, read Advanced Linux Programming and carefully syscalls(2) related man
pages.
Study for inspiration the source code of the Linux kernel, of GNU libc, of musl-libc. They all are open source projects, so feel free to contribute to them and to improve them.
But I bet that in practice using popen
, or stdin
, or reading from std::cin
won't add much overhead.
You could also increase the stdio buffer with setvbuf(3).
See also this question.
(If you read from stdin the file descriptor is STDIN_FILENO
which is 0)
You might be interested by time(7), vdso(7), syscalls(2)
You certainly should read documentation of GCC and this draft report.
You could use machine learning techniques to optimize performance.
Look into the MILEPOST GCC and Ctuning projects. Consider joining the RefPerSys one. Read of course Understanding machine learning: From theory to algorithms ISBN 978-1-107-05713-5