简体   繁体   English

用于广泛数学计划的C ++或Python?

[英]C++ or Python for an Extensive Math Program?

I'm debating whether to use C++ or Python for a largely math-based program. 我正在讨论是否使用C ++或Python作为基于数学的程序。

Both have great math libraries, but which language is generally faster for complex math? 两者都有很棒的数学库,但对于复杂的数学,哪种语言通常更快?

You could also consider a hybrid approach. 您也可以考虑采用混合方法。 Python is generally easier and faster to develop in, specially for things like user interface, input/output etc. Python通常更容易,更快速地开发,特别是用户界面,输入/输出等。

C++ should certainly be faster for some math operations (although if your problem can be formulated in terms of vector operations or linear algebra than numpy provides a python interface to very efficient vector manipulations). 对于某些数学运算,C ++肯定会更快(尽管如果你的问题可以用向量运算或线性代数表示,而不是numpy为非常有效的向量处理提供了python接口)。

Python is easy to extend with Cython, Swig, Boost Python etc. so one strategy is write all the bookkeeping type parts of the program in Python and just do the computational code in C++. Python很容易用Cython,Swig,Boost Python等扩展。因此,一种策略是用Python编写程序的所有簿记类型部分,然后用C ++编写计算代码。

I guess it is safe to say that C++ is faster. 我想可以说C ++更快。 Simply because it is a compiled language which means that only your code is running, not an interpreter as with python. 仅仅因为它是一种编译语言,这意味着只有你的代码在运行,而不是像python那样的解释器。

It is possible to write very fast code with python and very slow code with C++ though. 有可能用python编写非常快的代码,用C ++编写非常慢的代码。 So you have to program wisely in any language! 所以你必须用任何语言明智地编程!

Another advantage is that C++ is type safe, which will help you to program what you actually want. 另一个优点是C ++是类型安全的,它可以帮助您编写实际需要的内容。

A disadvantage in some situations is that C++ is type safe, which will result in a design overhead. 在某些情况下的缺点是C ++是类型安全的,这将导致设计开销。 You have to think (maybe long and hard) about function and class interfaces, for instance. 例如,你必须考虑(可能很长很难)关于函数和类接口。

I like python for many reasons. 我喜欢python有很多原因。 So don't understand this a plea against python. 所以不要理解这是对python的请求。

It all depends if faster is "faster to execute" or "faster to develop". 这一切都取决于更快“执行速度更快”还是“开发速度更快”。 Overall, python will be quicker for development, c++ faster for execution. 总的来说,python的开发速度更快,c ++的执行速度更快。 For working with integers (arithmetic), it has full precision integers, it has a lot of external tools (numpy, pylab...) My advice would be go python first, if you have performance issue, then switch to cpp (or use external libraries written in cpp from python, in an hybrid approach) 对于使用整数(算术),它有完整的精度整数,它有很多外部工具(numpy,pylab ...)我的建议是先python,如果你有性能问题,然后切换到cpp(或使用用python编写的cpp外部库,采用混合方法)

There is no good answer, it all depends on what you want to do in terms of research / calculus 没有好的答案,这完全取决于你在研究/微积分方面想做什么

It goes witout saying that C++ is going to be faster for intensive numeric computations. 毫无疑问,对于密集的数值计算,C ++会更快。 However, there are so many pre-existing libraries out there (written in C/C++/Haskell etc..), with Python wrappers - it's just more convenient to utilise the convenience of Python and let the existing libraries carry the load. 但是,有很多预先存在的库(使用C / C ++ / Haskell等编写),使用Python包装器 - 利用Python的便利性并让现有的库承担负载更方便。

One comprehensive system is http://www.sagemath.org and a fairly interesting link is the components it uses at http://sagemath.org/links-components.html . 一个综合系统是http://www.sagemath.org ,一个相当有趣的链接是它在http://sagemath.org/links-components.html上使用的组件。

A system with numpy / scipy and pandas from my experience is normally sufficient for most things. 根据我的经验,一个拥有numpy / scipypandas系统通常足以满足大多数事情的需要。

Use the one you like better (and you should like python better :)). 使用你更喜欢的那个(你应该更喜欢python :))。

In either case, any math-intensive computations should be carried out by existing libraries - which aren't language dependent (usually BLAS / LAPACK are used to perform the actual math). 在任何一种情况下,任何数学密集型计算都应该由现有的库执行 - 这些库不依赖于语言(通常使用BLAS / LAPACK来执行实际的数学运算)。 If you choose to go with python, use numpy for calculations. 如果你选择使用python,请使用numpy进行计算。

Edit: From your comments, it seems that you are very concerned with the speed of your program. 编辑:从您的评论中,您似乎非常关心程序的速度。 The only way to know for sure how much time is wasted by the high level pythonic code is to profile your program (for example, use ipython with run -p). 确定高级pythonic代码浪费了多少时间的唯一方法是分析您的程序(例如,使用运行-p的ipython)。

In most cases, you will find that the high level stuff takes about 10% of the total running time, and therefore switching from python to C++ will only improve that 10% by some factor, for a total gain of perhaps 5% in running time. 在大多数情况下,您会发现高级别的东西大约占总运行时间的10%,因此从python切换到C ++只能将某个因素提高10%,总运行时间可能增加5% 。

I sincerely doubt that Google and Stanford don't know C++. 我真诚地怀疑谷歌和斯坦福不懂C ++。

"Generally faster" is more than just language. “通常更快”不仅仅是语言。 Algorithms can make or break a solution, regardless of what language it's written in. A poor choice written in C++ and be beaten by Java or Python if either makes a better algorithm choice. 算法可以创建或解决一个解决方案,无论它是用什么语言编写的。用C ++编写的糟糕选择如果有更好的算法选择,可以被Java或Python打败。

For example, an in-memory, single CPU linear algebra library will have its doors blown in by a parallelized version done properly. 例如,一个内存中的单CPU线性代数库将通过正确完成的并行版本进入其中。

An implicit algorithm may actually be slower than an explicit one, despite time step stability restrictions, because the latter doesn't have to invert a matrix. 尽管时间步长稳定性限制,隐式算法实际上可能比显式算法慢,因为后者不必反转矩阵。 This is often true for hyperbolic partial differential equations. 对于双曲型偏微分方程,这通常是正确的。

You shouldn't care about "generally faster". 你不应该关心“通常更快”。 You ought to look deeply into the problem you're trying to solve and the algorithms used to solve it. 您应该深入研究您尝试解决的问题以及用于解决问题的算法。 You'll do better that way than a blind language choice. 你会比盲目的语言选择做得更好。

I would go with Python running on Java platform. 我会选择在Java平台上运行的Python。 This approach is implemented in DataMelt program . 这种方法在DataMelt程序中实现。 Algorithm that call Java libraries from Python can be faster, since JVM optimizes the code for you. 从Python调用Java库的算法可以更快,因为JVM会为您优化代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM