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++的:
ContractedBlock.gif ExpandedBlockStart.gif 针对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#的:
ContractedBlock.gif ExpandedBlockStart.gif 针对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 }

对比结果见下图:

2010062521234081.png

 

c/c++的:
ContractedBlock.gif ExpandedBlockStart.gif 针对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#的:
ContractedBlock.gif ExpandedBlockStart.gif 针对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 }

对比结果见下图:

2010062521251174.png

 

c/c++的:
ContractedBlock.gif ExpandedBlockStart.gif 针对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#的:
ContractedBlock.gif ExpandedBlockStart.gif 针对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 }

对比结果见下图:

2010062521254353.png

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

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

 

4.数学函数的运算能力

c/c++的:
ContractedBlock.gif ExpandedBlockStart.gif 常用数学函数
 
    
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#的:
ContractedBlock.gif ExpandedBlockStart.gif 常用数学函数
 
    
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 }

 

对比结果如下图:
2010062521310897.png
通过数据,我们发现c#在计算sin、cos、tan、log、sqrt等数学函数方面 明显
优于c/c++。大家以后注意了哦……

5.I/O操作能力

c/c++的:
ContractedBlock.gif ExpandedBlockStart.gif 写文件、读文件
 
     
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#的:
ContractedBlock.gif ExpandedBlockStart.gif 写文件、读文件
 
     
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 }

 

对比结果见下图:
2010062521354229.png
通过数据,我们发现c#的性能略优于c/c++。

6.数组运算能力

c/c++的:
ContractedBlock.gif ExpandedBlockStart.gif 数组基本操作
 
     
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#的:
ContractedBlock.gif ExpandedBlockStart.gif 数组基本操作
 
     
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 }

 

对比结果见下图:
2010062607205518.png
通过数据发现,在数组基本操作方面c#较弱(大家都能知道为什么!)。

7.异常处理能力

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

8.STL vs FCL

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

9.算法HeapSort

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

10.矩阵乘

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

11.嵌套for循环

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

12.字符串连接

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

13.总和对比

这里的总和指的是前面所有项的时间加和,对比结果见下图:
2010062608084854.png
通过数据发现,在一般的应用中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写成。有兴趣的读者点击这里下载源代码。

转载于:https://www.cnblogs.com/xiaoweiz/archive/2010/06/26/1765428.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: C是一种流行的编程语言,由Dennis Ritchie在20世纪70年代开发。C语言被广泛用于系统编程、操作系统、网络编程、游戏开发等领域。C语言简单高效,可以直接操作计算机的硬件,具有较高的性能和可移植性。C语言的语法和结构被很多其他编程语言所借鉴和扩展,如C++、Java、Python等。 ### 回答2: C指的是计算机编程语言C。C语言是一种通用的、高级的编程语言,由美国计算机科学家丹尼斯·里奇于20世纪70年代初设计和开发。C语言具有简洁、高效、灵活、可移植等特点,被广泛应用于操作系统、嵌入式系统、游戏开发、图形处理等领域。 C语言的特点包括: 1. 简洁高效:C语言的语法结构简单、紧凑,易于理解和学习。与其他高级语言相比,C语言的执行效率更高。 2. 灵活可移植:C语言可以在不同的操作系统和平台上编写,并且可以通过简单的修改源代码实现跨平台运行。 3. 强大的控制能力:C语言提供了丰富的控制结构和运算符,可以实现复杂的逻辑和算法。 4. 易于扩展和维护:C语言支持模块化编程,可以将代码分成多个模块,提高代码的可读性、可维护性和重用性。 5. 丰富的函数库:C语言拥有广泛的标准函数库,包括输入输出、字符串处理、数学计算等功能,可以大大简化编程过程。 总之,C语言是一种功能强大、灵活高效的编程语言,是计算机科学教育和软件开发中必学的基础语言之一。无论是初学者还是专业程序员,掌握C语言对于理解计算机底层原理、开发高效的程序以及进一步学习其他编程语言都有着重要的意义。 ### 回答3: C是一种高级的编程语言,它由贝尔实验室的Dennis Ritchie在20世纪70年代开发。C语言在计算机编程领域有着广泛的应用,尤其是在系统级编程和嵌入式系统开发方面。C语言的设计目标是提供一种效率高、跨平台、可移植性强的编程语言。 C语言有许多特点使其成为一种流行的编程语言。首先,C语言具有简洁而紧凑的语法,使得编写代码变得相对容易。其次,C语言提供了丰富的运算符和控制结构,使得程序员能够更灵活地控制程序的执行流程。另外,C语言还支持指针,这使得程序员能够直接访问内存地址,从而实现更高效的内存管理。此外,C语言还有丰富的标准库和函数,方便程序员进行各种常见的操作。 C语言广泛用于开发各种应用程序,包括操作系统、嵌入式系统、游戏、编译器等。由于C语言具有高效性和可移植性,很多常见的编程语言,例如C++、Java等,都是在C语言的基础上发展而来的。 C语言的学习对于初学者来说可能有一定的难度,因为它需要了解指针、内存管理等概念。但是一旦掌握了C语言,你就可以更加深入地理解计算机系统的底层原理,这对于日后的编程发展非常有帮助。 总之,C语言是一种功能强大的编程语言,它在计算机编程领域有着广泛的应用,并且拥有丰富的特性和库函数。掌握C语言对于学习编程和深入理解计算机系统原理非常有帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值