05年时,在微软的Rico Mariani做了一次实际应用的C#和C++的性能比较。事情起源于微软著名的元老Raymond Chen(在下敬仰的超级牛人)用C++写了一个英汉词典程序,来描述讲解优化C++程序的过程。他从一个代码简单直观的版本开始,不断测试优化,直到达到满意的性能。Rico Mariani使用相同的方式,但是使用C#做了相同的程序。结果是,Mariani的最初的简单直观版本的速度几乎是Chen的最初C++版本的十倍;而对于优化的终极版本,Chen重写了文件IO,重写了字符串类,使用了自定义的内存分配器,使得C++版本的速度是C#版本的两倍。
简单的说C#的设计目标之一就是可以使简单直观的代码具有较高的效率,而且它确实实现了这个目标;而C++允许我们使用任何手段来提高效率,只要愿意做出足够的努力。
原文:
A few years ago, Rico Mariani (at Microsoft) did really interesting performance comparison between C# and C++.
It started when Raymond Chen (one of the most important engineers at Microsoft, and in the CS universe generally) wrote a Chinese/English dictionary application in C++ to illustrate his process for optimizing C++ code. He started with a simple, intuitive version of the code, and then he incrementally profiled and optimized the bottlenecks until he was happy with the performance.
Rico Mariani took a similar tack, writing a simple, intuitive version in C#, and then optimizing its bottlenecks.
Surprisingly, Mariani's original C# implementation is almost ten times as fast as Chen's original C++ implementation, without having to resort to any counterintuitive hacks.
Eventually, Chen rewrote the file I/O library, implemented his own string class, and used a custom memory allocator. Only AFTER jumping through all those hoops, the C++ version was about twice as fast as the C# version.
The moral of the story is that C++ gives you the tools to write blazingly fast software, if you're willing to do a lot of non-standard programming, and use a LOT of hacks.
C#, on the other hand, lets you write really really fast software right out of the gate, without resorting to any oddball shenanigans. But if you want to reach maximum possible performance, you will hit a wall sooner with C# than you would with C++. Additionally, a piece of software in C# will almost always have greater memory requirements than an equivalent C++ program.
There's a summary of the story on Coding Horror, here:
http://blogs.msdn.com/ricom/archive/2005/05/10/performance-quiz-6-chinese-english-dictionary-reader.aspx
And you can read the whole play-by-play, here (follow all the links at the top of the page):
http://blogs.msdn.com/ricom/archive/2005/05/19/420158.aspx
简单的说C#的设计目标之一就是可以使简单直观的代码具有较高的效率,而且它确实实现了这个目标;而C++允许我们使用任何手段来提高效率,只要愿意做出足够的努力。
原文:
A few years ago, Rico Mariani (at Microsoft) did really interesting performance comparison between C# and C++.
It started when Raymond Chen (one of the most important engineers at Microsoft, and in the CS universe generally) wrote a Chinese/English dictionary application in C++ to illustrate his process for optimizing C++ code. He started with a simple, intuitive version of the code, and then he incrementally profiled and optimized the bottlenecks until he was happy with the performance.
Rico Mariani took a similar tack, writing a simple, intuitive version in C#, and then optimizing its bottlenecks.
Surprisingly, Mariani's original C# implementation is almost ten times as fast as Chen's original C++ implementation, without having to resort to any counterintuitive hacks.
Eventually, Chen rewrote the file I/O library, implemented his own string class, and used a custom memory allocator. Only AFTER jumping through all those hoops, the C++ version was about twice as fast as the C# version.
The moral of the story is that C++ gives you the tools to write blazingly fast software, if you're willing to do a lot of non-standard programming, and use a LOT of hacks.
C#, on the other hand, lets you write really really fast software right out of the gate, without resorting to any oddball shenanigans. But if you want to reach maximum possible performance, you will hit a wall sooner with C# than you would with C++. Additionally, a piece of software in C# will almost always have greater memory requirements than an equivalent C++ program.
There's a summary of the story on Coding Horror, here:
http://blogs.msdn.com/ricom/archive/2005/05/10/performance-quiz-6-chinese-english-dictionary-reader.aspx
And you can read the whole play-by-play, here (follow all the links at the top of the page):
http://blogs.msdn.com/ricom/archive/2005/05/19/420158.aspx
BenjiSmith
Saturday, December 08, 2007