. Net console output buffer

Source: Internet
Author: User

Doing some ad hoc benchmarks, I found that my compute/IO intensive program was significantly slower in. net than in native C ++ (here's a more rigorous test showing the same thing ). however, when the IO was taken out (console. write), the test showed that the computation in. net and in native C ++ was nearly the same (with C ++ still having a slight edge ).

My. Net Io code looked like this:

Static VoidOutputit (Int[] Vect ){
For(IntI = 0; I <vect. length; ++ I ){
If(I> 0)Console. Write(",");
Console. Write(Vect [I]);
}
Console. writeline ();
}

In tracking down this disparity, Courteney van den Berg noticed that, unlike STL Io streams in native C ++ ,. net console output is not buffered. A simple manual buffering using a stringbuilder brings the performance. net back in line with native C ++:

Static VoidOutputit (Int[] Vect ){
Stringbuilder sb =NewStringbuilder ();
For(IntI = 0; I <vect. length; ++ I ){
If(I> 0)SB. append(",");
SB. append(Vect [I]);
}
Console. writeline (SB );
}

Instead of buffering manually, I cocould have turned on buffered console Io by reopening console output with a buffer size:

static void main ( string [] ARGs) {
using (streamwriter bufferoutput = New streamwriter (console. openstandardoutput (1024) {
console. setout (bufferoutput);
// use console. write (via outputit )...
}
// remaining buffered console output flushed
}

// console output now buffered
static void outputit ( int [] vect) {
for ( int I = 0; I If (I> 0) console. write (",");
console. write (vect [I]);
}< br> console. writeline ();
}

Why isn' t console output buffered by default? Unlike in C ++, which has deterministic finalization, in. net, console output wocould have to be manually flushed or closed (Closing causes a flush ). failure to do so can cause console output to be incomplete at the end of a program. notice our careful use of the "using" block in the code above, causing the output stream to be closed at the end of the block and the remaining buffered output to be flushed, even in the face of exceptions. forgetting to use the "using" block or forgetting to manually flush or close in a Finally block, can cause console output to be lost. rather than imposing the new burden of manually closing the console output ,. net tables ts opted to turn off buffering by default, while still leill you turn it on at will.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.