Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么使用/clr:safe选项编译c++/cli程序会使被引用dll导出的方法比使用/clr选项更快?_C#_.net_C++ Cli_Interop_Clr - Fatal编程技术网

C# 为什么使用/clr:safe选项编译c++/cli程序会使被引用dll导出的方法比使用/clr选项更快?

C# 为什么使用/clr:safe选项编译c++/cli程序会使被引用dll导出的方法比使用/clr选项更快?,c#,.net,c++-cli,interop,clr,C#,.net,C++ Cli,Interop,Clr,我在Visual Studio developer命令提示符窗口中使用以下命令编写了一个编译为dll程序集的简单C类: csc/target:library/out:testdll.dll testdll.cs 以下是C文件testdll.cs的代码结构: < >我用以下命令编译这个C++程序: cl/clr testcpp.cpp 如果我运行testcpp.exe程序。我得到了这样的东西: 测试…完成了。AClass.DoSomething平均执行时间为1.253 us 现在我用以下命令重新编

我在Visual Studio developer命令提示符窗口中使用以下命令编写了一个编译为dll程序集的简单C类:

csc/target:library/out:testdll.dll testdll.cs

以下是C文件testdll.cs的代码结构:

< >我用以下命令编译这个C++程序:

cl/clr testcpp.cpp

如果我运行testcpp.exe程序。我得到了这样的东西:

测试…完成了。AClass.DoSomething平均执行时间为1.253 us

现在我用以下命令重新编译我的C++程序: cl/clr:安全测试cpp.cpp

请注意,dll尚未重新编译!自第一次执行以来,它没有更改

我运行新编译的testcpp.exe程序,得到以下结果:

测试…完成了。AClass.DoSomething平均执行时间为0.612 us

有人能解释一下为什么调用C++程序的/CLR:Sturny选项可以改变未重新编译的DLL的ACLAS.DOOMETHORY方法的执行时间吗??!!!p> 多谢各位


皮埃尔·弗朗索瓦·库兰(Pierre François Culand)

你发布的代码需要一纳秒的时间才能执行,这没有帮助。在DoSomething中做一些非琐碎的事情时,我没有看到任何强烈的信号,生成的机器代码完全相同。这当然是应该的。始终注意调试器对抖动优化器的影响。工具+选项、调试、常规、取消选中Superss JIT优化选项。1.253µs约为0.612µs的两倍。这可能是由于秒表的分辨率。在方法中执行更多工作以获得更高的值,并检查
using System;
using System.Diagnostics;

namespace Test
{
    public class AClass
    {

        AClass()
        {
        }

        private void DoSomething()
        {
           // Something I want to optimize to minimize execution time
           //...
        }

        // Calls the AClass.DoSomething() method 1'000'000 times and displays the 
        // average execution time of the method AClass.DoSomething() in micro-seconds 
        // on the console. 
        public static void Test()
        {
            AClass obj = new AClass();
            Stopwatch sw = new Stopwatch();
            Console.Write("Testing...");
            sw.Reset();
            sw.Start();
            for (int i = 1; i<=1000000; i++)
                obj.DoSomething();
            sw.Stop();
            Console.Write("done.");
            Console.WriteLine(" AClass.DoSomething() average execution time is {0}us", 
                              ((float)sw.ElapsedMilliseconds / 1000));
            Console.ReadLine();
        }
    }
}
#using "testdll.dll"
void main()
{
    Test::AClass::Test();
}