zoukankan      html  css  js  c++  java
  • 64位/32位 C++/C# 数学计算性能对比测试

    在下面的网址看到了使用MS的CL、gcc、Intel的icl、PGI的pgcc及Codegear的bcc 几个不同编译器编译的C/C++ 程序性能对比,结论是Intel的编译器性能最高。

    http://www.zxbc.cn/html/20081119/67961.html

    同样把这段Intel的SDK中的代码迁移到C#中比较一下

    我的笔记本是:Intel Core4 P8700 2.53G的CPU, 4G内存,Win7 64bit系统,VS2010自带的编译器

    对于代码略作调整和注释

    C++代码

    //intel的性能测试例子
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <time.h> 
    #include <math.h> 
    
    //为cin cout 提供
    #include <iostream>
    using namespace std;
    
    #define INTEG_FUNC(x) fabs(sin(x)) //计算公式
    
    double dclock(void); 
    
    int main(void) 
    { 
    	unsigned int i, j, N; 
    	double step, x_i, sum; 
    	double start, finish, duration, clock_t; 
    	double interval_begin = 0.0; 
    	double interval_end = 2.0 * 3.141592653589793238; 
    
    	start = clock(); //初始时间
    
    	printf(" \n"); 
    	printf(" Number of中文 | Computed Integral | \n"); //Win7下中文显示正常
    	printf(" Interior Points | | \n"); 
    
    	for (j=2;j<27;j++) 
    	{  
    		N = 1 << j;
    
    		step = (interval_end - interval_begin) / N; 
    		sum = INTEG_FUNC(interval_begin) * step / 2.0; 
    
    		for (i=1;i<N;i++) 
    		{ 
    			x_i = i * step; 
    			sum += INTEG_FUNC(x_i) * step; 
    		} 
    
    		sum += INTEG_FUNC(interval_end) * step / 2.0; 
    
    		//printf(" %10d | %14e | \n", N, sum); 
            printf(" %14e  \n", sum); 
    	} 
    
    	finish = clock(); //结束时间
    	duration = (finish - start); 
    	printf(" \n"); 
    	printf(" Application Clocks = %10e \n", duration); 
    	printf(" \n"); 
    
    	int tempA;
    	cin>>tempA;
    
    	return 0; 
    } 

    默认编译参数,都是Release编译后,拿出exe文件独立运行

    32bit C++     6338ms

    C# 代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int time = System.Environment.TickCount;  //添加计时器
    
                #region
                int i, j, N;
                double step, x_i, sum;
                double start, finish, duration, clock_t;
                double interval_begin = 0.0;
                double interval_end = 2.0 * 3.141592653589793238;
    
                for (j = 2; j < 27; j++)
                {
                    N = 1 << j; 
                    step = (interval_end - interval_begin) / N;
                    sum = Math.Abs(Math.Sin(interval_begin)) * step / 2.0;
    
                    for (i = 1; i < N; i++)
                    {
                        x_i = i * step;
                        sum += Math.Abs(Math.Sin(x_i)) * step;
                    }
    
                    sum += Math.Abs(Math.Sin(interval_end)) * step / 2.0;
                    Console.Write(sum.ToString()+"\r\n");
                }
    
                Console.Write((System.Environment.TickCount - time).ToString());
                Console.ReadLine();
                #endregion
            }
        }
    }

    32bit C#  命令行   5382ms

    32bit C#  WinForm  5351ms

    都是重复测试了5次,最大最小误差少于30ms

    从左到依次为:32bit C++、 32bit C#命令行、 32bit C#WinForm 

    image

    C#的竟然比C++快了1秒。

    再看看64bit的,64bit C++ 3696ms, 64bit C#  5382 ms

    从左到右依次为:

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

    image

    可见该程序64bit 编译时,C++的性能大幅提升,C#的几乎不变。

    两个计算精度应该相同,C++是因为输出的格式科学计数法隐藏了后面的小数

    结论:

    image

    1. C# 在WinForm和命令行中,数学计算性能相当

    2. 32bit下C#的性能还不错,若能在64bit下编译器也能充分优化达到C++那样的提升就好了。

    杨韬的学习备忘录 YTYT2002YTYT

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

  • 相关阅读:
    CodeForces 659F Polycarp and Hay
    CodeForces 713C Sonya and Problem Wihtout a Legend
    CodeForces 712D Memory and Scores
    CodeForces 689E Mike and Geometry Problem
    CodeForces 675D Tree Construction
    CodeForces 671A Recycling Bottles
    CodeForces 667C Reberland Linguistics
    CodeForces 672D Robin Hood
    CodeForces 675E Trains and Statistic
    CodeForces 676D Theseus and labyrinth
  • 原文地址:https://www.cnblogs.com/ytyt2002ytyt/p/2261104.html
Copyright © 2011-2022 走看看