C#/C++/CLI运行效率测试之二: Native C++调用Native C++ DLL

 本文分为两方面

一.  Native C++调用Native C++ DLL 并测试运行时间

二. 完全由C# 实现运算代码 并测试运行时间

详细过程:

一:

至于如何获取EfficiencyNativeCPPDLL.dll 和 EfficiencyNativeCPPDLL.lib 以及生成项目EfficiencyNativeCPPDLL请参见前面的文章 C#通过CLR/C++调用Native CPP 类

本文讲述在VS2010中本地C++调用C++类库以进行效率测试


1. 常见Win32控制台应用程序 ConsoleCPPInvokeDLL

  

2.  选择下一步点击完成就可以了

3  对EfficiencyNativeCPPDLL.lib的调用

在项目ConsoleCPPInvokeDLL中的资源文件上\右击添加现有项可选择 EfficiencyNativeCPPDLL.lib

  

4  书写测试主体文件 

CPPInvokeDLL.cpp

#include "stdafx.h"
#include "EfficiencyNativeCppDll.h"
#include 
#define NITER 201

int _tmain(int argc, _TCHAR* argv[])
{
	int i;
	clock_t start, stop;

	EfficiencyNativeCppDll* effPro = new EfficiencyNativeCppDll();
	effPro->InitPositions();
	effPro->UpdatePositions();

	start=clock();
	for( i=0; iPot = 0.0;	
		
		/*
		//低效率模式
		effPro->ComputePot();
		if (i%10 == 0) printf("%5d: Potential: %10.3f\n", i, effPro->Pot);
		*/
		
		//高效率模式 ??不晓得为什么这样的速度会快那么多 快了近20倍
		if (i%10 == 0) printf("%5d: Potential: %10.3f\n", i, effPro->ComputePot());
		
		effPro->UpdatePositions();
	}
	stop=clock();
	printf ("Seconds = %10.9f\n",(double)(stop-start)/ CLOCKS_PER_SEC);
	
	delete effPro;

	getchar();
	
}

测试结果:


二: C#代码实现及测试结果:

1. 

EffCompute.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;



namespace ConsoleEfficiencyCS
{
    public class EffCompute
    {
        public double Pot{get;set;}
        private int DIMS = 3;
        private int NPARTS = 1000;
        private double[,] _r ;
        private Random _rand;

        public EffCompute()
        {
            _r = new double[DIMS, NPARTS];
            _rand = new Random();
        }
       

        public void InitPositions()
        {
            for(int i = 0; i < DIMS; i++)
	        {
		        for (int j = 0; j < NPARTS; j++)
		        {
			        _r[i, j] = 0.5 + _rand.NextDouble();
		        }
	        }
        }

        public void UpdatePositions()
        {
            for (int i = 0; i < DIMS; i++)
            {
                for (int j = 0; j < NPARTS; j++)
                {
                    _r[i, j] -= 0.5 + _rand.NextDouble();
                }
            }
        }

        public double ComputePot()
        {
            double distx, disty, distz, dist;
            double pot;
            distx = 0;
            disty = 0;
            distz = 0;
            pot = 0;

            for (int i = 0; i < NPARTS; i++)
            {
                for (int j = 0; j < i - 1; j++)
                {
                    distx = Math.Pow((_r[0, j] - _r[0, i]), 2);
                    disty = Math.Pow((_r[1, j] - _r[1, i]), 2);
                    distz = Math.Pow((_r[2, j] - _r[2, i]), 2);
                    dist = Math.Sqrt(distx + disty + distz);
                    pot += 1.0 / dist;
                }
            }

            this.Pot = pot;

            return pot;
        }
    }

    
}

2.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleEfficiencyCS
{
    class Program
    {
        static void Main(string[] args)
        {
            EffCompute provider = new EffCompute();
            const int NITER = 201;

            provider.InitPositions();
            provider.UpdatePositions();

            int start = Environment.TickCount;
            for (int i = 0; i < NITER; i++)
            {
                provider.Pot = 0.0;

                //低效模式
               /* provider.ComputePot();
                if (i % 10 == 0)
                    Console.WriteLine("{0}: Potential: \t {1}", i, provider.Pot);
                 */

                //高效模式                
                if (i % 10 == 0)
                    Console.WriteLine("{0}: Potential: \t {1}", i, provider.ComputePot());

                provider.UpdatePositions();

            }
            int stop = Environment.TickCount;

            Console.WriteLine("Seconds = {0,10}", (double)(stop - start) / 1000);

            Console.ReadKey();
        }
    }
}

3 运算结果:

  



综合以上两篇文章可见 在均是Release情况下分别对

C#调用CLI/C++转而调用 Native C++ Dll ;,本地C++调用 和 CS的.Net实现

可以看出 

本地C++调用效率最高

C#调用CLI/C++转而调用 Native C++ Dll的效率其次 可以说跟本地C++调用差不多

而CS的实现效率大约相差20倍.



你可能感兴趣的:(.Net,C++/CLR)