64-bit/32-bit C ++/C # mathematical computing performance comparison test

Source: Internet
Author: User

 

In the following url: http://www.bkjia.com/kf/201111/112209.html saw the use of ms cl, gcc, Intel icl, PGI pgcc and Codegear bcc several different compilers compiled C/C ++ program performance comparison, the conclusion is that Intel has the highest compiler performance.

 

Migrate the code in this Intel SDK to C # for comparison.

 

My notebook is: Intel Core4 P8700 2.53G CPU, 4G memory, Win7 64bit system, VS2010 built-in Compiler

 

 

 

Slightly adjust and comment the code

 

C ++ code

01 // intel performance test example

 

02 # include <stdio. h>

 

03 # include <stdlib. h>

 

04 # include <time. h>

 

05 # include <math. h>

 

06

 

07 // provided for cin cout

 

08 # include <iostream>

 

09 using namespace std;

 

10

 

11 # define INTEG_FUNC (x) fabs (sin (x) // calculate the formula

 

12

 

13 double dclock (void );

 

14

 

15 int main (void)

 

16 {

 

17 unsigned int I, j, N;

 

18 double step, x_ I, sum;

 

19 double start, finish, duration, clock_t;

 

20 double interval_begin = 0.0;

 

21 double interval_end = 2.0*3.141592653589793238;

 

22

 

23 start = clock (); // initial time

 

24

 

25 printf ("\ n ");

 

26 printf ("Number of Chinese | Computed Integral | \ n"); // Chinese in Win7 is displayed normally

 

27 printf ("Interior Points | \ n ");

 

28

 

29 for (j = 2; j <27; j ++)

 

30 {

 

31 N = 1 <j;

 

32

 

33 step = (interval_end-interval_begin)/N;

 

34 sum = INTEG_FUNC (interval_begin) * step/2.0;

 

35

 

36 for (I = 1; I <N; I ++)

 

37 {

 

38 x_ I = I * step;

 

39 sum + = INTEG_FUNC (x_ I) * step;

 

40}

 

41

 

42 sum + = INTEG_FUNC (interval_end) * step/2.0;

 

43

 

44 // printf ("% 10d | % 14e | \ n", N, sum );

 

45 printf ("% 14e \ n", sum );

 

46}

 

47

 

48 finish = clock (); // End Time

 

49 duration = (finish-start );

 

50 printf ("\ n ");

 

51 printf ("Application Clocks = % 10e \ n", duration );

 

52 printf ("\ n ");

 

53

 

54 int tempA;

 

55 cin> tempA;

 

56

 

57 return 0;

 

58}

 

 

 

 

 

The default compilation parameters are all Release compiled and run independently from the exe file.

 

32bit C ++ 6338 ms

 

 

 

C # code

01 using System;

 

02 using System. Collections. Generic;

 

03 using System. Linq;

 

04 using System. Text;

 

05

 

06 namespace ConsoleApplication1

 

07 {

 

08 class Program

 

09 {

 

10 static void Main (string [] args)

 

11 {

 

12 int time = System. Environment. TickCount; // Add a timer

 

13

 

14 # region

 

15 int I, j, N;

 

16 double step, x_ I, sum;

 

17 double start, finish, duration, clock_t;

 

18 double interval_begin = 0.0;

 

19 double interval_end = 2.0*3.141592653589793238;

 

20

 

21 for (j = 2; j <27; j ++)

 

22 {

 

23 N = 1 <j;

 

24 step = (interval_end-interval_begin)/N;

 

25 sum = Math. Abs (Math. Sin (interval_begin) * step/2.0;

 

26

 

27 for (I = 1; I <N; I ++)

 

28 {

 

29 x_ I = I * step;

 

30 sum + = Math. Abs (Math. Sin (x_ I) * step;

 

31}

 

32

 

33 sum + = Math. Abs (Math. Sin (interval_end) * step/2.0;

 

34 Console. Write (sum. ToString () + "\ r \ n ");

 

35}

 

36

 

37 Console. Write (System. Environment. TickCount-time). ToString ());

 

38 Console. ReadLine ();

 

39 # endregion

 

40}

 

41}

 

42}

 

 

 

32bit C # command line 5382 ms

 

32bit C # WinForm 5351 ms

 

 

All tests are repeated for 5 times, and the maximum and minimum error is less than 30 ms.

 

 

From left to right: 32bit C ++, 32bit C # command line, 32bit C # WinForm

 

 

 

 

 

C # is one second faster than C ++.

 

 

 

Let's look at 64bit, 64bit C ++ 3696 ms, 64bit C #5382 ms

 

From left to right:

 

64bit C ++, 32 bitC ++, 64bit C #

 

 

 

 

 

It can be seen that when the program is 64-bit compiled, the performance of C ++ is greatly improved, and the performance of C # is almost unchanged.

 

The two calculation accuracy should be the same. C ++ is because the output format scientific notation hides the decimals

 

 

 

Conclusion:

 

 

 

1. C # In WinForm and command line, the mathematical computing performance is equivalent

 

2. The performance of C # In 32bit is good. If the 64-bit compiler can be fully optimized, it would be better to achieve the improvement of C ++.

 

Yang Tao's learning memorandum YTYT2002YTYT

 

Http://www.cnblogs.com/ytyt2002ytyt/archive/2011/11/24/2261104.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.