C#/C++/CLI运行效率测试之二 Native C++挪用Native C++ DLL
副标题#e#
本文分为两方面
一.Native C++挪用Native C++ DLL 并测试运行时间
二. 完全由C# 实现运算代码 并测试运行时间
具体进程:
一:
至于如何获取EfficiencyNativeCPPDLL.dll 和 EfficiencyNativeCPPDLL.lib 以及生成项目EfficiencyNativeCPPDLL
本文报告在VS2010中当地C++挪用C++类库以举办效率测试
1. 常见Win32节制台应用措施 ConsoleCPPInvokeDLL
2.选择下一步点击完成绩可以了
3 对EfficiencyNativeCPPDLL.lib的挪用
在项目ConsoleCPPInvokeDLL中的资源文件上\右击添加现有项可选择 EfficiencyNativeCPPDLL.lib
#p#副标题#e#
二: C#代码实现及测试功效:
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; } } }
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倍.
作者:csdn博客 Cheviko