zoukankan      html  css  js  c++  java
  • C#的性能到底有多差?

    这篇文章带领大家来看看c#的性能问题。当然了,作为比较的选手是c/c++。

    首先说说测试环境:

    操作系统:win7 旗舰版

    内存:2GB

    硬盘:160GB

    处理器:Intel Pentium Dual CPU T2330 @ 1.60GHZ

    本文打算基于以下几个方面讨论:

    1.CPU使用情况;

    2.内存使用情况;

    3.基本类型的四则运算能力;

    4.数学函数运算能力;

    5.I/O操作能力;

    6.数组运算能力;

    7.异常处理能力;
    8.STL vs FCL;
    9.算法HeapSort;
    10.矩阵乘;
    11.嵌套for循环;
    12.字符串连接。
    接下来,我们一个一个详细地说:(注,测试程序都是直接运行编译后的exe)

    1.CPU使用情况

    在程序的整个运行过程中,两者的CPU占用情况都在50%上下浮动,算是不分上下。

    2.内存使用情况

    这里列一 组程序执行过程中我记录的数据:
    c/c++:216kb,832kb,904kb,944kb,1336kb。
    c#:1972kb,1980kb,2000kb,2372kb,3024kb,5156kb。
    通过这组数据,不难发现,在内存使用方面c#可算是一败涂地。注:
    希望有人能解释下c#为什么会出现这种一路飙升的情况呢?!

    3.基本类型的四则运算能力

    费话不多说了,上代码:(注,所有的图中时间单位均为毫秒ms)
    c/c++的:
    针对int的
    1 double intArithmetic(">int intMax)
    2 {
    3 double elapsedTime;
    4 clock_t stopTime;
    5 int intResult = 1;
    6 int i = 1;
    7
    8 clock_t startTime = clock();
    9 while (i < intMax)
    10 {
    11 intResult -= i++;
    12 intResult += i++;
    13 intResult *= i++;
    14 intResult /= i++;
    15 }
    16 stopTime = clock();
    17
    18 elapsedTime = (stopTime - startTime) / (CLOCKS_PER_SEC / (double) 1000.0);
    19 printf("Int arithmetic elapsed time: %1.0f ms with intMax of %ld\n", elapsedTime, intMax);
    20 printf(" i: %d\n", i);
    21 printf(" intResult: %d\n", intResult);
    22 return elapsedTime;
    23 }
    c#的:
    针对int的
    1 static long intArithmetic(int intMax)
    2 {
    3 long elapsedMilliseconds;
    4 int intResult = 1;
    5 int i = 0;
    6
    7 stopwatch.Start();
    8 while (i < intMax)
    9 {
    10 intResult -= i++;
    11 intResult += i++;
    12 intResult *= i++;
    13 intResult /= i++;
    14 }
    15 elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
    16 stopwatch.Reset();
    17
    18 Console.WriteLine("Int arithmetic elapsed time: " + elapsedMilliseconds +
    19 " ms with max of " + intMax);
    20 Console.WriteLine(" i: " + i);
    21 Console.WriteLine(" intResult: " + intResult);
    22 return elapsedMilliseconds;
    23 }

    对比结果见下图:

    c/c++的:
    针对double的
    1 double doubleArithmetic(double doubleMin, double doubleMax)
    2 {
    3 double elapsedTime;
    4 clock_t stopTime;
    5 clock_t startTime = clock();
    6
    7 double doubleResult = doubleMin;
    8 double i = doubleMin;
    9 while (i < doubleMax)
    10 {
    11 doubleResult -= i++;
    12 doubleResult += i++;
    13 doubleResult *= i++;
    14 doubleResult /= i++;
    15 }
    16
    17 stopTime = clock();
    18 elapsedTime = (stopTime - startTime) / (CLOCKS_PER_SEC / (double) 1000.0);
    19 printf("Double arithmetic elapsed time: %1.0f ms with doubleMin %.15f, doubleMax %.15f\n", elapsedTime, doubleMin, doubleMax);
    20 printf(" i: %f\n", i);
    21 printf(" doubleResult: %.15f\n", doubleResult);
    22 return elapsedTime;
    23 }
    c#的:
    针对double的
    1 static long doubleArithmetic(double doubleMin, double doubleMax)
    2 {
    3 long elapsedMilliseconds;
    4 double doubleResult = doubleMin;
    5 double i = doubleMin;
    6
    7 stopwatch.Start();
    8 while (i < doubleMax)
    9 {
    10 doubleResult -= i++;
    11 doubleResult += i++;
    12 doubleResult *= i++;
    13 doubleResult /= i++;
    14 }
    15 elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
    16 stopwatch.Reset();
    17
    18 Console.WriteLine("Double arithmetic elapsed time: " + elapsedMilliseconds +
    19 " ms with min of " + doubleMin + ", max of " + doubleMax);
    20 Console.WriteLine(" i: " + i);
    21 Console.WriteLine(" doubleResult: " + doubleResult);
    22 return elapsedMilliseconds;
    23 }

    对比结果见下图:

    c/c++的:
    针对long的
    1 double longArithmetic(long long longMin, long long longMax)
    2 {
    3 double elapsedTime;
    4 clock_t stopTime;
    5 clock_t startTime = clock();
    6
    7 long long longResult = longMin;
    8 long long i = longMin;
    9 while (i < longMax)
    10 {
    11 longResult -= i++;
    12 longResult += i++;
    13 longResult *= i++;
    14 longResult /= i++;
    15 }
    16
    17 stopTime = clock();
    18 elapsedTime = (stopTime - startTime) / (CLOCKS_PER_SEC / (double) 1000.0);
    19 printf("Long arithmetic elapsed time: %1.0f ms with longMax %I64d\n", elapsedTime, longMax);
    20 printf(" i: %I64d\n", i);
    21 printf(" longResult: %I64d\n", longResult);
    22 return elapsedTime;
    23 }
    c#的:
    针对long的
    1 static long longArithmetic(long intMin, long intMax)
    2 {
    3 long elapsedMilliseconds;
    4 long intResult = intMin;
    5 long i = intMin;
    6
    7 stopwatch.Start();
    8 while (i < intMax)
    9 {
    10 intResult -= i++;
    11 intResult += i++;
    12 intResult *= i++;
    13 intResult /= i++;
    14 }
    15 elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
    16 stopwatch.Reset();
    17
    18 Console.WriteLine("long arithmetic elapsed time: " + elapsedMilliseconds +
    19 " ms with min of " + intMin + ", max of " + intMax);
    20 Console.WriteLine(" i: " + i);
    21 Console.WriteLine(" intResult: " + intResult);
    22 return elapsedMilliseconds;
    23 }

    对比结果见下图:

    通过以上数据,发现两者对基本数据类型的操作性能差不多,属于同一个数量级。

    同时,注意到,c#针对int型的性能要优于c/c++。

    4.数学函数的运算能力

    c/c++的:
    常用数学函数
    1 double trig(double trigMax)
    2 {
    3 double elapsedTime;
    4 clock_t stopTime;
    5 clock_t startTime = clock();
    6
    7 double sine;
    8 double cosine;
    9 double tangent;
    10 double logarithm;
    11 double squareRoot;
    12
    13 double i = 0.0;
    14 while (i < trigMax)
    15 {
    16 sine = sin(i);
    17 cosine = cos(i);
    18 tangent = tan(i);
    19 logarithm = log10(i);
    20 squareRoot = sqrt(i);
    21 i++;
    22 }
    23
    24 stopTime = clock();
    25 elapsedTime = (stopTime - startTime) / (CLOCKS_PER_SEC / (double) 1000.0);
    26 printf("Trig elapsed time: %1.0f ms with max of %1.0f\n", elapsedTime, trigMax);
    27 printf(" i: %f\n", i);
    28 printf(" sine: %.15f\n", sine);
    29 printf(" cosine: %.15f\n", cosine);
    30 printf(" tangent: %.15f\n", tangent);
    31 printf(" logarithm: %.15f\n", logarithm);
    32 printf(" squareRoot: %.15f\n", squareRoot);
    33 return elapsedTime;
    34 }
    c#的:
    常用数学函数
    1 static long trig(double trigMax)
    2 {
    3 long elapsedMilliseconds;
    4
    5 double sine = 0.0D;
    6 double cosine = 0.0D;
    7 double tangent = 0.0D;
    8 double logarithm = 0.0D;
    9 double squareRoot = 0.0D;
    10 double i = 0.0D;
    11
    12 stopwatch.Start();
    13 while(i < trigMax)
    14 {
    15 sine = Math.Sin(i);
    16 cosine = Math.Cos(i);
    17 tangent = Math.Tan(i);
    18 logarithm = Math.Log10(i);
    19 squareRoot = Math.Sqrt(i);
    20 i++;
    21 }
    22 elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
    23 stopwatch.Reset();
    24
    25 Console.WriteLine("Trig elapsed time: " + elapsedMilliseconds +
    26 " ms with max of " + trigMax);
    27 Console.WriteLine(" i: " + i);
    28 Console.WriteLine(" sine: " + sine);
    29 Console.WriteLine(" cosine: " + cosine);
    30 Console.WriteLine(" tangent: " + tangent);
    31 Console.WriteLine(" logarithm: " + logarithm);
    32 Console.WriteLine(" squareRoot: " + squareRoot);
    33 return elapsedMilliseconds;
    34 }
    对比结果如下图:
    通过数据,我们发现c#在计算sin、cos、tan、log、sqrt等数学函数方面明显
    优于c/c++。大家以后注意了哦……

    5.I/O操作能力

    c/c++的:
    写文件、读文件
    1 double io(int ioMax)
    2 {
    3 double elapsedTime;
    4 clock_t stopTime;
    5 clock_t startTime = clock();
    6
    7 FILE *stream;
    8 stream = fopen("F:\\TestC.txt", "w");
    9 int i = 0;
    10 while (i++ < ioMax)
    11 {
    12 fputs("abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefgh\n", stream);
    13 }
    14 fclose(stream);
    15
    16 char readLine[100];
    17 stream = fopen("F:\\TestC.txt", "r");
    18 i = 0;
    19 while (i++ < ioMax)
    20 {
    21 fgets(readLine, 100, stream);
    22 }
    23 fclose(stream);
    24
    25 stopTime = clock();
    26 elapsedTime = (stopTime - startTime) / (CLOCKS_PER_SEC / (double) 1000.0);
    27 printf("I/O elapsed time: %1.0f ms with max of %ld\n", elapsedTime, ioMax);
    28 printf(" i: %d\n", i);
    29 printf(" readLine: %s\n", readLine);
    30
    31 return elapsedTime;
    32 }
    c#的:
    写文件、读文件
    1 static long io(int ioMax)
    2 {
    3 long elapsedMilliseconds;
    4
    5 String fileName = "F:\\TestCSharp.txt";
    6 String textLine = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefgh";
    7 int i = 0;
    8 String myLine = "";
    9
    10 stopwatch.Start();
    11 try
    12 {
    13 StreamWriter streamWriter = new StreamWriter(fileName);
    14 while (i++ < ioMax)
    15 {
    16 streamWriter.WriteLine(textLine);
    17 }
    18 streamWriter.Close();
    19
    20 i = 0;
    21 StreamReader streamReader = new StreamReader(fileName);
    22 while (i++ < ioMax)
    23 {
    24 myLine = streamReader.ReadLine();
    25 }
    26 }
    27 catch (IOException e)
    28 {
    29 System.Console.Write(e.Message);
    30 }
    31
    32
    33 elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
    34 stopwatch.Reset();
    35
    36 Console.WriteLine("IO elapsed time: " + elapsedMilliseconds +
    37 " ms with max of " + ioMax);
    38 Console.WriteLine(" i: " + i);
    39 Console.WriteLine(" myLine: " + myLine);
    40 return elapsedMilliseconds;
    41 }
    对比结果见下图:
    通过数据,我们发现c#的性能略优于c/c++。

    6.数组运算能力

    c/c++的:
    数组基本操作
    1 double array(int n)
    2 {
    3 int i, k, *x, *y;
    4
    5 double elapsedTime;
    6 clock_t stopTime;
    7 clock_t startTime = clock();
    8 x = new int[n];
    9 y = new int[n];
    10
    11 for (i = 0; i < n; i++) {
    12 x[i] = i + 1;
    13 y[i] = 0;
    14 }
    15 for (k=0; k<1000; k++) {
    16 for (i = n-1; i >= 0; i--) {
    17 y[i] += x[i];
    18 }
    19 }
    20
    21 stopTime = clock();
    22 elapsedTime = (stopTime - startTime) / (CLOCKS_PER_SEC / (double) 1000.0);
    23 printf("array elapsed time: %1.0f ms - %ld %ld\n", elapsedTime, y[0], y[n-1]);
    24
    25 delete[] x;
    26 delete[] y;
    27
    28 return elapsedTime;
    29 }
    c#的:
    数组基本操作
    1 static long array(int n)
    2 {
    3 long elapsedMilliseconds;
    4 int i, j, k;
    5 int[] x;
    6 int[] y;
    7
    8 stopwatch.Start();
    9 if(n < 1) n = 1;
    10
    11 x = new int[n];
    12 y = new int[n];
    13
    14 for (i = 0; i < n; i++)
    15 {
    16 x[i] = i + 1;
    17 y[i] = 0;
    18 }
    19 for (k = 0; k < 1000; k++ )
    20 for (j = n-1; j >= 0; j--)
    21 y[j] += x[j];
    22
    23 elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
    24 stopwatch.Reset();
    25
    26 Console.WriteLine("Array elapsed time: " + elapsedMilliseconds + " ms - "+ y[0].ToString() + " " + y[n-1].ToString());
    27 return elapsedMilliseconds;
    28 }
    对比结果见下图:
    通过数据发现,在数组基本操作方面c#较弱(大家都能知道为什么!)。

    7.异常处理能力

    由于这部分代码稍多,所以就不在这贴了,一多后台就崩溃了(在写这篇文章其间博客园后台不知崩溃了n次,对此很无语……)。
    后面会给出所有源码的下载地址。
    对比结果见下图:
    通过数据,发现c/c++在这方面远胜于c#,可是大家不要就此止步,再想想为什么。注:
    可参考Jeffrey Richter的CLR via C#中有关异常的讨论。

    8.STL vs FCL

    郁闷啊!不知道为什么,一提交网站不停地崩溃,我写的东西不停地全没。没有办法,代码就不贴了。直接上结果。
    这里,通过map<T1,T2>和Dictionary<T1,T2>的两组对比和一组Vector<T>和List<T>的对比。
    第一组对比结果见下图:
    这里涉及到的基本操作包括,插入和测试key是否存在。通过数据,发现c#的Dictionary<T1,T2>
    的性能远远优于c/c++(10倍有余)。
    第二组对比结果见下图:
    通过数据发现c#版的字典性能远优于c/c++中的。
    Vector<T>和List<T>的对比结果见下图:
    通过数据发现两者性能不相上下。

    9.算法HeapSort

    对比结果见下图:
    通过数据发现,c/c++要优于c#(看代码发现罪魁祸首在于数组操作)。

    10.矩阵乘

    对比结果见下图:
    通过数据发现,c/c++的性能几乎是c#的两倍。截止到目前,我们应该知道
    c#在数组、矩阵操作方面是个弱项(为什么?)。大家在以后的工作学习中要学会
    扬长避短哦……

    11.嵌套for循环

    对比结果见下图:
    通过数据发现,c#略差于c/c++。

    12.字符串连接

    大家都知道,字符串连接是个耗时的操作,现在让我们来看看两者的表现,对比结果见下图:
    令人意外的是,c#在这方面的性能要优于c/c++(几乎2倍有余)。

    13.总和对比

    这里的总和指的是前面所有项的时间加和,对比结果见下图:
    通过数据发现,在一般的应用中c#的性能能达到c/c++的70%-80%。
    作为托管代码来说这已经很不错了,你说呢?

    总结

        写了这么多了,也许有人要问我了,你到底想说什么呢?其实,作为语言,我们要懂得什么情况下用哪个。我的观点是,
    精通一门,熟悉或了解另一门。怎么说呢?如果您已经工作了,那么很容易做决定,您工作中不停要用到的语言自然是您
    应该精通的了。如果您和我一样还是学生的话,那也很好办,凭自己的喜好呗,但不要太过执着于语言本身。比如说我,
    花了大量时间在c#上,今年暑假要去公司实习(大半会用到c++),所以你也要了解c++,不是吗?
        现在的社会,一切都讲求速度。特别是在软件行业更是如此。软件不仅要求运行速度,很多时候更多的是开发速度,而且是第一个
    遇到的问题。不是有哪位大牛说过嘛,先让它run起来,再让它run地更快。结合上面的形式,一个项目中即需要懂c#的也需要懂c++的。
    下面的建议摘自别人的博客原文:
    Except for writing time-critical blocks of code, prefer C#. Write all your algorithmic code in C++ (not VC++ .NET), compile it into a dll and call that using a Dll Interop through C#. This should balance the performance. This technique is not new or not invented by me or anyone. It's similar the old age C programming vs Assembly, where people on one camp fight assembly programming is faster and the other camp stating C is easier to develop and then people started using assembly embedded within a C program for time-critical applications using an asm block.

    History repeats...!

    在一个真正的软件项目中我们要做到开发速度与运行速度的折中。另外,建议大家看一下《Framework Design Guidelines, Conventions,Idioms,and Patterns

    for Reusable .NET Libraries》,这本书深刻阐述了.NET的设计过程,其认真程度(包括一个命名)令人折服。

    参考

    1. http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html
    2. http://www.codeproject.com/KB/cs/CSharpVsCPP.aspx
    3. http://msdn.microsoft.com/en-us/library/ms173196(VS.80).aspx
    4. http://dotnet.sys-con.com/node/46342
    5. http://www.techgalaxy.net/Docs/Dev/5ways.htm

        注:本文基本上基于参考1写成。有兴趣的读者点击这里下载源代码。

  • 相关阅读:
    java导出pdf格式文档
    本地文件夹选择框
    将文件解除占用
    Windows 进入上帝模式窗口
    Windows 10 系统获取密钥方法
    CentOS7 systemctl 命令
    一键立即息屏
    定时关闭程序
    CentOS 7 FTP的安装与配置
    SQL基础
  • 原文地址:https://www.cnblogs.com/xiaoweiz/p/1765428.html
Copyright © 2011-2022 走看看