ctypes 与 C 扩展

发布于 2024-12-14 12:20:11 字数 332 浏览 2 评论 0原文

我有一些用 C 语言编写的游戏项目函数。这些函数被多次调用(每秒大约 2000-4000 次)。这些函数是用 C 语言编写的,以提高原始速度。

现在,将这些函数包含到 Python 中的最简单方法是使用 ctypes。另一种方法是围绕这些函数编写 Python 的 C 扩展(这需要相当多的额外工作)。所以我想知道,不包括DLL的初始加载,ctypes的开销有多大?


我正在使用 Python 2.7(标准 CPython 版本),并且我不想使用像 Cython 这样的外部库。

我知道这个问题之前已经被问过,但我还没有看到太多关于这两个选项之间的性能比较的信息。

I have a few functions written in C for a game project. These functions get called quite a lot (about 2000-4000 times per second). The functions are written in C for raw speed.

Now, the easiest way for me to include these functions into Python is to use ctypes. The alternative is to write a C extension to Python around these functions (which takes quite a bit of extra effort). So I wondered, not including the initial loading of the DLL, how big is the overhead of ctypes?


I'm using Python 2.7 (the standard CPython release), and I do not want to use an external library like Cython.

I know this question has been asked before, but I haven't seen much information about the performance comparison between the two options.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅我要高飞 2024-12-21 12:20:11

我比较了 C 扩展与 ctypes 包装器的性能。在我的特定测试中,差异约为 250 倍。由于对 C 库进行了多次调用,因此 ctypes 包装器也在执行 Python 代码。 C 库的运行时间非常短,这使得 Python 代码的额外开销更加显着。所以这个比率对你来说可能会有所不同,但对我来说很重要。

I've compared the performance of a C extension vs. a ctypes wrapper. In my particular test, the difference was about 250x. There were multiple calls into the C library so the ctypes wrapper was also executing Python code. The running time for the C library was very short which made the extra overhead for Python code even more significant. So the ratio will likely be different for you but was significant in my case.

猫九 2024-12-21 12:20:11

直接 C 编码的接口有可能更快。瓶颈是从 Python 到 C 的接口,编组参数和结果可能涉及复制字符串或将 Python 列表转换为 C 数组或从 C 数组转换。如果您有一个循环进行数百次这样的调用,并且某些数据不必为每个调用单独编组,那么您所要做的就是用 C 重新编码循环,您也许能够大大减少瓶颈。 ctypes 没有给你这个选项:你所能做的就是直接调用现有的函数。

当然,这完全取决于您正在调用什么类型的函数以及您正在传递什么类型的数据。也许您无法减少开销,在这种情况下,我仍然希望 ctypes 变慢,但可能不会显着。

最好的方法是将各种方式编写的代码示例放在一起并对其进行基准测试。否则的话,变量太多,无法给出明确的答案。

The directly C coded interface has the potential to be much much faster. The bottleneck is the interface from Python to C and marshalling arguments and results may for example involve copying strings or converting Python lists to/from C arrays. If you have a loop that makes several hundred of these calls and some of the data doesn't have to be marshalled separately for each call then all you have to do is recode the loop in C and you may be able to massively reduce the bottleneck. ctypes doesn't give you that option: all you can do is call the existing functions directly.

Of course that all depends on exactly what sort of functions you are calling and what sort of data you are passing around. It may be that you can't reduce the overheads in which case I would still expect ctypes to be slower but perhaps not significantly.

Your best best would be to put together some sample of your code written each way and benchmark it. Otherwise there are just too many variables for a definitive answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文