一次C#和C++的實際應用效能比較

來源:互聯網
上載者:User
05年時,在微軟的Rico Mariani做了一次實際應用的C#和C++的效能比較。事情起源於微軟著名的元老Raymond Chen(在下敬仰的超級牛人)用C++寫了一個英漢詞典程式,來描述講解最佳化C++程式的過程。他從一個代碼簡單直觀的版本開始,不斷測試最佳化,直到達到滿意的效能。Rico Mariani使用相同的方式,但是使用C#做了相同的程式。結果是,Mariani的最初的簡單直觀版本的速度幾乎是Chen的最初C++版本的十倍;而對於最佳化的終極版本,Chen重寫了檔案IO,重寫了字串類,使用了自訂的記憶體 Clerk,使得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

BenjiSmith
Saturday, December 08, 2007 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.