ctypes的VS C扩展 [英] ctypes vs C extension

查看:131
本文介绍了ctypes的VS C扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C语言编写的一个游戏项目的一些功能。这些函数被调用了很多(每秒约2000-4000倍)。该函数是用C语言编写的原始速度。

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.

现在,对我来说,有这些功能到Python的最简单的方法是使用 ctypes的。另一种方法是写一个C扩展到Python围绕这些功能(这需要相当多的额外的努力)。所以我在想,不包括DLL的初始加载,有多大 ctypes的

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?

我使用Python 2.7(标准CPython的释放),我不希望使用一个外部库就像用Cython。

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.

推荐答案

我比较了C扩展与一个ctypes包装的性能。在我的特殊检验,差异约为250倍。有多个呼叫到C库,以便在ctypes的包装也被执行Python code。运行时间为C库是很短这使得对于Python code更显著的额外开销。因此,比例很可能会为你不同,但在我的情况显著。

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.

这篇关于ctypes的VS C扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆