C#与C/C++ 性能比较

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++216kb832kb904kb944kb1336kb

c#1972kb1980kb2000kb2372kb3024kb5156kb

通过这组数据,不难发现,在内存使用方面c#可算是一败涂地。注:

希望有人能解释下c#为什么会出现这种一路飙升的情况呢?!

 

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

费话不多说了,上代码:(注,所有的图中时间单位均为毫秒ms

c/c++的:

针对int

 1double 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

 1staticlong 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

 1double 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

 1staticlong 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

 1double longArithmetic(longlong longMin, longlong longMax)
 2{
 3    double elapsedTime;
 4    clock_t stopTime;
 5    clock_t startTime = clock();
 6
 7    longlong longResult = longMin;
 8    longlong 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

 1staticlong 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++的:

常用数学函数

 1double 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#的:

常用数学函数

 1staticlong 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#在计算sincostanlogsqrt等数学函数方面明显

优于c/c++。大家以后注意了哦……

5.I/O操作能力

c/c++的:

写文件、读文件

 1double 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#的:

写文件、读文件

 1staticlong 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++的:

数组基本操作

 1double 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 = newint[n];
 9    y =newint[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#的:

数组基本操作

 1staticlong 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 =newint[n];
12            y =newint[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 RichterCLR 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的设计过程,其认真程度(包括一个命名)令人折服。

参考

·         http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html

·         http://www.codeproject.com/KB/cs/CSharpVsCPP.aspx

·         http://msdn.microsoft.com/en-us/library/ms173196(VS.80).aspx

·         http://dotnet.sys-con.com/node/46342

·         http://www.techgalaxy.net/Docs/Dev/5ways.htm

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

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在计算机编程,C和C++都是非常重要的编程语言。它们都是被广泛应用于系统开发和高性能应用程序的常用语言。 C是一种被广泛使用的、面向过程的编程语言。它由Dennis Ritchie在20世纪70年代初开发,一开始被用于UNIX操作系统的开发。C具有简洁的语法结构和丰富的函数库,使得它非常适合用于系统级编程和嵌入式设备开发。C还可以编写高效的代码,并具有较少的内存占用,因此它对于资源有限的设备非常合适。许多其他编程语言,如C++、Java和Python等,都是在C的基础上开发的。 C++是在C语言的基础上扩展而来的编程语言。它由Bjarne Stroustrup在20世纪80年代开发,旨在为C语言添加面向对象的特性。C++继承了C的简洁性和高效性,并添加了类、继承、封装和多态等面向对象的功能。C++还具有强大的标准模板库(STL),提供了许多现成的容器和算法,使得程序开发更加快速和简便。C++广泛应用于游戏开发、图形界面以及大规模软件系统等领域。 虽然C和C++在某些方面相似,但它们也有一些重要的区别。C语言更加简洁,具有更小的语法和更少的特性,因此更加适合编写较低级别的、对性能要求较高的程序。而C++则更加强大和灵活,具有更多的特性和功能,使得它更适合开发大型软件系统和应用程序。 总体而言,C和C++都是非常重要的编程语言。选择使用哪个取决于项目的具体需求和个人的偏好。无论是选择C还是C++,掌握这些编程语言的基本概念和语法结构都是非常有益的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值