You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
前言
最近优化一个由 C# 编写的解密程序,经过一系列技巧优化后,提升幅度在 50% 左右,但还是达不到一个很好的速度(此时 CPU 占用率仅为 5% 左右),然而又不想用 C++ 重写一遍……
想到上一篇帖子的多进程加速,python 的 multiprocessing 模块实在是太方便了~ 既然如此,何不利用 Native AOT 来导出原生的 C 库,进而使用 python 进行调用呢?
编写类库
新建一个类库项目
起个名字
这里我用了 .NET8 ,最低可以是 .NET7
编辑
NativeLibraryTest.csproj
文件,开启 AOT 编译和允许使用 unsafe 代码:编写调用程序,并使用
UnmanagedCallersOnly
显式声明导出函数名:在项目目录下执行以下命令进行编译:
等待编译完成后即可得到
NativeLibraryTest.dll
,可以看到其导出函数:python 调用
调用就很简单了,直接上代码:
速度对比
单进程解密:
多进程解密:
Beta Was this translation helpful? Give feedback.
All reactions