我写了两个等价的方法:
static bool F<T>(T a, T b) where T : class
{
return a == b;
}
static bool F2(A a, A b)
{
return a == b;
}
Run Code Online (Sandbox Code Playgroud)
时差:
00:00:00.0380022
00:00:00.0170009
测试代码:
var a = new A();
for (int i = 0; i < 100000000; i++)
F<A>(a, a);
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int i = 0; i < 100000000; i++)
F2(a, a);
Console.WriteLine(DateTime.Now - dt);
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么?
在下面的评论中,dtb*显示CIL:
IL for F2: ldarg.0, ldarg.1, …Run Code Online (Sandbox Code Playgroud) 我使用NuGet包UnmanagedExports构建了一个C#DLL(MyTestDll):
[DllExport("Test", CallingConvention = CallingConvention.Cdecl)]
public static string Test(string name)
{
return "hi " + name + "!";
}
Run Code Online (Sandbox Code Playgroud)
我通过ctypes DLL导入Python使用它:
path = "C:\\Temp\\Test"
os.chdir(path)
dll = ctypes.WinDLL("MyTestDll.dll")
f = dll.Test
f.restype = ctypes.c_char_p
print f('qqq')
Run Code Online (Sandbox Code Playgroud)
这只是一种幻想,它有效.
然后,我又添加了一个DLL(NoSenseDll):
namespace NoSenseDll
{
public class NoSenseClass
{
public static int Sum(int a, int b)
{
return a + b;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我开始使用这个NoSenseDll来实现MyTestDll:
[DllExport("Test", CallingConvention = CallingConvention.Cdecl)]
public static string Test(string name)
{
return NoSenseDll.NoSenseClass.Sum(4, 5).ToString();
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,它不起作用.Python说:
WindowsError: [Error …Run Code Online (Sandbox Code Playgroud)