python - 与手写 C 相比,pypy 是否可以快速处理线程和套接字?

标签 python c multithreading sockets pypy

与手写 C 相比,pypy 是否可以快速处理线程和套接字?与普通 python 相比?

我只是想尝试一下,但有问题的 python 代码是为我不是管理员的小型计算机集群编写的。我在这里问是因为我尝试使用 google 只提供了与 cython、unladen swallow 等的比较,如果这不太可能工作,我不想打扰管理员。

我实际上并不需要 pypy 才能像 C 语言那样擅长;我希望使用它,因为现在解释器的开销完全盖过了我正在尝试计时的计算。我只需要 pypy 让我接近手写 C。

最佳答案

Does pypy handle threads and sockets quickly compared to hand written C?

没有。通常情况相同或更差。

PyPy 保留了 CPython 所拥有的全局解释器锁 (GIL)。这意味着 native 线程无法并行运行 Python 代码。 Python 线程还具有额外的语义,但这是有代价的。许多同步都围绕着 Python 线程的启动、关闭和线程对象的跟踪。相比之下,C 线程启动速度更快,使用成本更低,并且可以完全并行运行。

高效的套接字处理需要尽量减少等待下一个套接字事件的时间。由于 PyPy 的线程模型仍然受 GIL 约束,这意味着从阻塞套接字调用返回的线程在获得 GIL 之前无法继续。等效的 C 代码通常执行得更快,并且可以更快地返回等待套接字事件。

Compared to normal python?

是的。但不多。

由于上述原因,除了 JIT 和其他开销导致的偶尔峰值外,PyPy 将需要更少的 CPU 时间来处理等效代码。因此,线程和套接字处理速度更快,响应更快。

I would just try it, but the python code in question was written for a small cluster of computers on which I am not an admin. I'm asking here because my attempts to google only provided comparisons to cython, unladen swallow, ect., and I don't want to bug the admin about it if this is unlikely to work.

如果您的代码受 CPU 限制,PyPy 只会显着提高性能。 PyPy 是 fastest我知道的 Python 实现 native 运行。你可以调查一些 other implementations ,或者如果真正的线程并行性对您来说是一个高优先级,请考虑编写 C 扩展。

I don't actually need pypy to be as good at C; I'm looking to use it because right now the interpreter's overhead is completely overshadowing the computation I'm trying to time. I just need pypy to get me in the neighborhood of handwritten C.

缩小与 C 的性能差距是目前 PyPy 的最大特点。我强烈建议您尝试一下。

关于python - 与手写 C 相比,pypy 是否可以快速处理线程和套接字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9450812/

相关文章:

python - 如何向 Plotly Express 条形图添加辅助 Y 轴?

python - 尝试用 map 中的值替换数据帧值时无法比较类型 'ndarray(dtype=int64)' 和 'str'

python - 删除对对象的引用,但如果在其他地方引用,则将对象保留在内存中

c - 星号和函数名之间的空格

python多处理池并不总是使用所有 worker

python - 在 Python2 中通过 scrapy 从网络读取 json

c - 如何正确使用 % 或 *(星号)符号作为 makefile 中的占位符?

澄清一些变量的初始值属于C中的存储类 'auto'

multithreading - sync.Mutex在并发Golang程序中是如何工作的

java - 除了互斥锁或垃圾收集之外还有哪些机制可以减慢我的多线程 Java 程序的速度?