Profiling my program and the function print is taking a lot of time to perform. How can I send ‘raw’ byte output directly to stdout instead of using fwrite, and making it faster (need to send all 9bytes in the print() at the same time to the stdout) ?
void print(){ unsigned char temp[9]; temp[0] = matrix[0][0]; temp[1] = matrix[0][1]; temp[2] = matrix[0][2]; temp[3] = matrix[1][0]; temp[4] = matrix[1][1]; temp[5] = matrix[1][2]; temp[6] = matrix[2][0]; temp[7] = matrix[2][1]; temp[8] = matrix[2][2]; fwrite(temp,1,9,stdout);
}
Matrix is defined globally to be a unsigned char matrix[3][3];
IO is not an inexpensive operation. It is, in fact, a blocking operation, meaning that the OS can preempt your process when you call
writeto allow more CPU-bound processes to run, before the IO device you’re writing to completes the operation.The only lower level function you can use (if you’re developing on a *nix machine), is to use the raw
writefunction, but even then your performance will not be that much faster than it is now. Simply put: IO is expensive.