August 19, 2003 spout

Buffering .NET Console Output

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 void OutputIt(int[] vect) {
  for (int i = 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 of .NET back in line with native C++:

static void OutputIt(int[] vect) {
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < vect.Length; ++i) {
    if( i > 0 ) sb.Append(”,“);
    sb.Append(vect[i]);
  }
  Console.WriteLine(sb);
}

Instead of buffering manually, I could 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 < vect.Length; ++i) {
    if( i > 0 ) Console.Write(”,“);
    Console.Write(vect[i]);
  }
  Console.WriteLine();
}

Why isn’t Console output buffered by default? Unlike in C++, which has deterministic finalization, in .NET, console output would 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 architects opted to turn off buffering by default, while still letting you turn it on at will.

August 18, 2003

Protect Your Friends’ & Families’ PCs

Here. Nobody reading this site needs any help protecting themselves from the blaster virus or any other kind, but those of you with families and friends having trouble, point them at Microsoft's Protect your PC site.
August 18, 2003

Microsoft Programming Languages

Here. Prashant Sridharan, a Senior Product Manager at Microsoft, details the uniqueness of VB.NET, C#, C++ and J# to help you pick the right tool for the right job.
August 17, 2003

PDC03 Birds of a Feather Sessions

Here. Propose your own BoF sessions for PDC03.
August 16, 2003 .net

Introducing Generics in the CLR

Here. Jason Clark on generics in the CLR. Can't wait!
August 15, 2003

The real goals of marketing?

Here. The one where I hope my clever-penned friend is wrong.
August 15, 2003 spout

The real goals of marketing?

I’ve long lamented that I have no clue what the basics of marketing are and therefore I’ve been at the mercy of actual marketing people whose skill I can’t judge. In fact, I so miss this important knowledge in my life that I interviewed for a marketing job at Microsoft (although I was in no danger of getting it). A *long* time friend of mine, Dave Stroble, said that I was too “customer-focused” to be in marketing, anyway, and I should be glad I’m not in that field. When I asked how that could be (isn’t customer focus the core of marketing?), he went on to say the following:

Marketing is not about giving the customer what he wants, or even finding out what the customer wants and trying to get engineering to create it. It’s about trying to sell the customer what you already have — whether that’s product, talent, or pre-conceived notions. If the needs of a customer occasionally overlap with an actual product, that’s merely random coincidence.

“Marketing people are customer-focused in the sense of always thinking about why customers aren’t buying enough stuff, and how to get them to buy more. You’re customer-focused in the sense of caring about what customers need, and helping them accomplish it, even if that doesn’t result in selling anything.

But don’t take it so hard. It’s not as if I said you were too honest to be a banker, or too smart to be a teacher. (God, what if girls thought you were too handsome to be sexy?)”

While I find this cleverly stated (even though I’m nowhere near too sexy for girls : ), I sure hope this isn’t the real goal of marketing. My hope is that it’s about taking a solution and letting folks that have the matching problem know so that you can trade your solution for their money and both consider yourselves lucky. Is this a naive view? Anyone with real marketing training want to chime in on the real goals of marketing?

August 14, 2003

What You Should Know About the Blaster Worm

Here. "At 11:34 A.M. Pacific Time on August 11, Microsoft began investigating a worm reported by Microsoft Product Support Services (PSS). A new worm commonly known as W32.Blaster.Worm has been identified that exploits the vulnerability that was addressed by Microsoft Security Bulletin MS03-026."

← Newer Entries Older Entries →