io - fastest way to print new line in std out in C language? -
so solving questions on codechef(programming competition platform) , specific question has lot of i/o every result printed in new line on console. without printing results in new line program execution takes .3 seconds new line taking around 1.3 seconds 1 second being limit program execution.
my question is possible print new line on console faster??
i using putchar_unlocked , using custom function output specified below. compiler gcc 4.8.1
#define pc(x) putchar_unlocked(x); inline void writeint (int n) { int n = n, rev, count = 0; rev = n; if (n == 0) { pc('0'); pc('\n'); return; } while ((rev % 10) == 0) { count++; rev /= 10; //obtain count of number of 0s } rev = 0; while (n != 0) { rev = (rev<<3) + (rev<<1) + n % 10; n /= 10; //store reverse of n in rev } while (rev != 0) { pc(rev % 10 + '0'); rev /= 10; } while (count--) pc('0'); pc('\n'); //this line prints new line , reason 1 second delay! }
by default stdout
line-buffered. means system call write
invoked if there newline in buffer.
change stdout buffering buffered:
setvbuf(stdout, null, _iofbf, 0);
and flush output @ end:
fflush(stdout);
Comments
Post a Comment