登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

秒大刀 博客

好好学习 天天向上

 
 
 

日志

 
 
 
 

C#和C++性能比较  

2009-05-04 11:50:15|  分类: C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

    从2005年学C#到现在,印象中一直是C++的性能要比C#的性能好,虽然微软官方宣称C#将来可能在某些应用上性能会超过C++,但至少在现在我们貌似还没有看到这一天的到来。.NET 比 C/C++ 慢是事实,Microsoft 提供的资料宣称 .NET 大约是 C/C++ 执行效率的 90%。XNA 则提供资料宣称 Managed DirectX 效率可以达到 Native DirectX 的 98%。

    今天偶尔看到《VC编译器和C#编译器的速度差异很大》一文,有点手痒就自己测了一把。平台VS2008,Release优化全开,C#采用3.5的framework,C++采用多字节字符集。测试结果如下:

不开启IO,测试100次:

C++动态内存 890ms
C++静态内存 812ms
C# 669ms

开启IO,测试单次:

C++动态内存流输出 76391ms
C++静态内存流输出 75796ms
C++动态内存CRT输出 31859ms
C++静态内存CRT输出 31984ms
C# 31790ms

    这下连我自己也哑口无言了,早上我告诉了老廖这个“严重”的消息,这确实很让人兴奋。

    当然仅此一例不能草率的说明C#的性能已经超过了C++,但绝对可以说在某些情况下,C#的性能绝对不会比C++差再也不能以性能的理由去拒绝C#,否则就是盲目崇拜了。可以认为C#已经是一门成熟的具有工业强度的优秀语言了

本测试用到的代码:

C#代码:

   using System;
   using System.Collections.Generic;
   using System.Text;
   namespace Kick
   {
       class Program
       {
           static void Main(string[] args)
           {
               int[] store;
               store = new int[10];
               int sum = 0;
               M.Cumput(90, 9, store);
               sum = M.sum2();
               Console.Write("总数:   {0}", sum);
               Console.ReadLine();
           }
       }
       public class M
       {
           public static int sum;
           public static int sum2()
           {
               return sum;
           }
           public static void Output(int[] store2)
           {
               for (int i = 9; i >= 0; --i)
               {
                   Console.Write("   {0}", store2[i]);
               }
               Console.WriteLine();
               sum++;
           }
           public static void Cumput(int score, int num, int[] store2)
           {
               if (score < 0 || score > (num + 1) * 10)
               {
                   return;
               }
               if (num == 0)
               {
                   store2[num] = score;
                   Output(store2);
                   return;
               }
               for (int i = 0; i <= 10; ++i)
               {
                   store2[num] = i;
                   Cumput(score - i, num - 1, store2);
               }
           }
       }
   }  

C++代码:

   #include<iostream>
   #include<iomanip>
   #include<cstdlib>
   using namespace std;
   static int sum=0;
   void output(int store[]);
   void comput(int score,int num,int store[]);
   int main()
   {
       system("color 9");
       int *store;
       store=new int[10];
       comput(90,9,store);
       cout<<"总的次数为:";
       cout<<sum;
       return 0;
   }
   void output(int store[])
   {
       for(int i=9;i>=0;--i)
       {
           cout<<setw(4)<<store[i];
       }
       cout<<endl;
       sum++;
   }
   void comput(int score,int num,int store[])
   {
       if(score<0||score>(num+1)*10)
       {
           return;
       }
       if(num==0)
       {
           store[num]=score;
           output(store);
           return;
       }
       for(int i=0;i<=10;++i)
       {
           store[num]=i;
           comput(score-i,num-1,store);
       }
   }  

其他参考:

    《C#, C++, Java性能对比

    《应用程序的性能: C# vs C/C++

    《Is Managed Code Slower Than Unmanaged Code?

  评论这张
 
阅读(7451)| 评论(11)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018