小编Dmi*_*try的帖子

C#中的通用与非通用性能

我写了两个等价的方法:

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)

c# generics performance templates

25
推荐指数
4
解决办法
1万
查看次数

使用非托管导出将C#DLL加载到Python中

我使用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)

.net c# python dll unmanaged

7
推荐指数
1
解决办法
1048
查看次数

标签 统计

c# ×2

.net ×1

dll ×1

generics ×1

performance ×1

python ×1

templates ×1

unmanaged ×1