这篇文章带领大家来看看c#的性能问题。当然了,作为比较的选手是c/c++。
首先说说测试环境:
操作系统:win7 旗舰版
内存:2GB
硬盘:160GB
处理器:Intel Pentium Dual CPU T2330 @ 1.60GHZ
本文打算基于以下几个方面讨论:
1.CPU使用情况;
2.内存使用情况;
3.基本类型的四则运算能力;
4.数学函数运算能力;
5.I/O操作能力;
6.数组运算能力;
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 }
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++的:
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 }
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++的:
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 }
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++。
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 }
对比结果如下图:
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 }
对比结果见下图:
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 }
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 }
对比结果见下图:
History repeats...!
在一个真正的软件项目中我们要做到开发速度与运行速度的折中。另外,建议大家看一下《Framework Design Guidelines, Conventions,Idioms,and Patterns
for Reusable .NET Libraries》,这本书深刻阐述了.NET的设计过程,其认真程度(包括一个命名)令人折服。
注:本文基本上基于参考1写成。有兴趣的读者点击这里下载源代码。