将python与c集成以提高性能是否合理? [英] Is it reasonable to integrate python with c for performance?

查看:48
本文介绍了将python与c集成以提高性能是否合理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢在几乎所有事情上都使用 python 并且我始终清楚,如果由于某种原因我在我的 python 代码中找到瓶颈(由于 python 的限制),我总是可以使用集成到我的 C 脚本中代码.

I like to use python for almost everything and always had clear in my mind that if for some reason I was to find a bottleneck in my python code(due to python's limitations), I could always use a C script integrated to my code.

但是,当我开始阅读关于如何操作的指南时集成python.作者在文章中说:

But, as I started to read a guide on how to integrate python. In the article the author says:

人们可能希望用 C 或 C++ 扩展 Python 的原因有多种,例如:

There are several reasons why one might wish to extend Python in C or C++, such as:

  • 调用现有库中的函数.
  • 向 Python 添加新的内置类型
  • 优化代码中的内循环
  • 向 Python 公开 C++ 类库
  • 在 C/C++ 应用程序中嵌入 Python

与性能无关.所以我又问了,为了性能,python 和 c 集成合理吗?

Nothing about performance. So I ask again, is it reasonable to integrate python with c for performance?

推荐答案

根据我的经验,很少需要使用 C 进行优化.我更喜欢完全在 Python 中找出瓶颈并改进这些领域的算法.使用哈希表、缓存和重新组织您的数据结构以满足未来的需求,对于加速您的程序具有惊人的潜力.随着您的程序的发展,您将更好地了解可以预先计算的材料类型,因此不要害怕返回并重做您的存储和算法.此外,寻找机会杀死一石两鸟",例如在渲染对象时对它们进行分类,而不是进行大量分类.

In my experience it is rarely necessary to optimize using C. I prefer to identify bottlenecks and improve algorithms in those areas completely in Python. Using hash tables, caching, and generally re-organizing your data structures to suit future needs has amazing potential for speeding up your program. As your program develops you'll get a better sense of what kind of material can be precalculated, so don't be afraid to go back and redo your storage and algorithms. Additionally, look for chances to kill "two birds with one stone", such as sorting objects as you render them instead of doing huge sorts.

当一切尽你所知时,我会考虑使用像 Psyco 这样的优化器.通过使用 Psyco 并在我的程序中添加一行代码,我体验到了 10 倍的性能提升.

When everything is worked to the best of your knowledge, I'd consider using an optimizer like Psyco. I've experienced literally 10x performance improvements just by using Psyco and adding one line to my program.

如果所有其他方法都失败了,请在适当的地方使用 C,然后您就会得到想要的.

If all else fails, use C in the proper places and you'll get what you want.

这篇关于将python与c集成以提高性能是否合理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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