NumPy 和 SciPy 有多少是用 C 语言编写的?

发布于 2024-08-13 21:25:00 字数 330 浏览 3 评论 0原文

NumPy 和/或 SciPy 的部分是用 C/C++ 编程的吗?

从 Python 调用 C 的开销与从 Java 和/或 C# 调用 C 的开销相比如何?

我只是想知道对于科学应用程序来说,Python 是否比 Java 或 C# 更好。

如果我查看 枪战,Python惨败。但我想这是因为他们在这些基准测试中没有使用第三方库。

Are parts of NumPy and/or SciPy programmed in C/C++?

And how does the overhead of calling C from Python compare to the overhead of calling C from Java and/or C#?

I'm just wondering if Python is a better option than Java or C# for scientific apps.

If I look at the shootouts, Python loses by a huge margin. But I guess this is because they don't use 3rd-party libraries in those benchmarks.

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

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

发布评论

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

评论(5

怎言笑 2024-08-20 21:25:00
  1. 我会质疑任何不显示每个实现的来源的基准(或者我错过了什么)?完全有可能这些解决方案中的一个或两个都编码错误,这将导致对其中一个或两种语言的性能进行不公平的评估。 [编辑] 哎呀,现在我看到了源代码。正如其他人指出的那样,它没有使用 NumPy/SciPy 库,因此这些基准不会帮助您做出决定。
  2. 我相信绝大多数 NumPy 和 SciPy 都是用 C 编写的,并封装在 Python 中以方便使用。
  3. 特定应用程序的开销可能取决于您使用这些语言中的任何一种所做的事情。

我使用 Python 进行数据处理和分析已经有几年了,所以我想说它确实适合我的目的。

您最终想实现什么目标?如果你想要一种快速的方法来开发可读的代码,Python 是一个很好的选择,而且对于你想要解决的任何问题来说,它的速度肯定足够快。

为什么不针对问题的一小部分进行一次 bash,并根据开发时间和运行时间对结果进行基准测试呢?然后你可以根据一些相关数据做出客观的决定......或者至少这就是我会做的:-)

  1. I would question any benchmark which doesn't show the source for each implementation (or did I miss something)? It's entirely possible that either or both of those solutions are coded badly which would result in an unfair appraisal of either or both language's performance. [Edit] Oops, now I see the source. As others have pointed out though, it's not using the NumPy/SciPy libraries so those benchmarks are not going to help you make a decision.
  2. I believe the vast majority of NumPy and SciPy is written in C and wrapped in Python for ease of use.
  3. It probably depends what you're doing in any of those languages as to how much overhead there is for a particular application.

I've used Python for data processing and analysis for a couple of years now so I would say it's certainly fit for purpose.

What are you trying to achieve at the end of the day? If you want a fast way to develop readable code, Python is an excellent option and certainly fast enough for a first stab at whatever it is you're trying to solve.

Why not have a bash at each for a small subset of your problem and benchmark the results in terms of development time and run time? Then you can make an objective decision based on some relevant data ...or at least that's what I'd do :-)

何以心动 2024-08-20 21:25:00

这里有一个更好的比较(不是基准,但显示了加速 Python)。 NumPy 大部分是用 C 编写的。Python 的主要优点是有很多方法可以非常轻松地使用 C(ctypes、swig、f2py)/C++(boost.python、weave)扩展代码.inline、weave.blitz) / Fortran (f2py) - 或者甚至只是通过向 Python 添加类型注释,以便将其处理为 C (cython)。我不认为有很多事情对于 C# 或 Java 来说是相对容易的——至少可以无缝地处理传递不同类型的数值数组(尽管我猜支持者会争论,因为他们没有 Python 的性能损失,所以不需要那么简单)到)。

There is a better comparison here (not a benchmark but shows ways of speeding up Python). NumPy is mostly written in C. The main advantage of Python is that there are a number of ways of very easily extending your code with C (ctypes, swig,f2py) / C++ (boost.python, weave.inline, weave.blitz) / Fortran (f2py) - or even just by adding type annotations to Python so it can be processed to C (cython). I don't think there are many things comparably easy for C# or Java - at least that so seemlessly handle passing numerical arrays of different types (although I guess proponents would argue since they don't have the performance penalty of Python there is less need to).

强者自强 2024-08-20 21:25:00

其中很多是用 C 或 Fortran 编写的。你可以用 C 语言重写热循环(或者使用无数种方法来加速 Python,boost/weave 是我最喜欢的),但这真的重要吗?

您的科学应用程序将运行一次。剩下的就是调试和开发,而这些在 Python 上会更快。

A lot of it is written in C or fortran. You can re-write the hot loops in C (or use one of the gazillion ways to speed python up, boost/weave is my favorite), but does it really matter?

Your scientific app will be run once. The rest is just debugging and development, and those can be much quicker on Python.

初相遇 2024-08-20 21:25:00

NumPy 的大部分都是用 C 语言编写的,但是 C 代码的很大一部分是“样板文件”,用于处理 Python/C 接口的所有脏细节。我认为对于 NumPy,C 与 Python 的比率约为 50/50 ATM。

我不太熟悉基于vm的底层细节,但我相信由于jvm和.clr的限制,接口成本会更高。 numpy 通常比类似环境更快的原因之一是内存表示以及数组如何在函数之间共享/传递。大多数环境(我认为也是 Matlab 和 R)使用 Copy-On-Write 在函数之间传递数组,而 NumPy 使用引用。但在例如 JVM 中这样做会很困难(因为如何使用指针等方面的限制)。这是可行的(存在 Jython 的 NumPy 的早期端口),但我不知道他们如何解决这个问题。也许 C++/Cli 会让这变得更容易,但我对该环境的经验为零。

Most of NumPy is in C, but a large portion of the C code is "boilerplate" to handle all the dirty details of the Python/C interface. I think the ratio C vs. Python is around 50/50 ATM for NumPy.

I am not too familiar with vm-based low-level details, but I believe the interface cost would be higher because of the restrictions put on the jvm and the .clr. One of the reason why numpy is often faster than similar environments is the memory representation and how arrays are shared/passed between functions. Whereas most environments (Matlab and R as well I believe) use Copy-On-Write to pass arrays between functions, NumPy use references. But doing so in e.g. the JVM would be hard (because of restrictions on how to use pointer, etc...). It is doable (an early port of NumPy for Jython exists), but I don't know how they solve this issue. Maybe C++/Cli would make this easier, but I have zero experience with that environment.

∝单色的世界 2024-08-20 21:25:00

这始终取决于您自己处理语言的能力,因此该语言能够生成快速的代码。根据我的经验,numpy 比好的 .NET 实现慢几倍。我预计 JAVA 也能快到类似的程度。他们的优化 JIT 编译器多年来得到了显着改进,并生成非常高效的指令。

另一方面,numpy 附带了一种更易于使用的语法,适合脚本语言。但如果涉及到应用程序开发,这些优势往往会变成障碍,您会渴望类型安全和企业 IDE。此外,C# 的语法差距已经缩小。 Java.NET。我个人倾向于 C#,因为它为多维数组提供了更好的语法,并且不知何故感觉更“现代”。但当然,这只是我的个人经历。

It always depends on your own capability to handle the langue, so the language is able to generate fast code. Out of my experience, numpy is several times slower then good .NET implementations. And I expect JAVA to be similar fast. Their optimizing JIT compilers have improved significantly over the years and produce very efficient instructions.

numpy on the other hand comes with a syntax wich is easier to use for those, which are attuned to scripting languages. But if it comes to application development, those advantages often turn to obstacles and you will yearn for typesafety and enterprise IDEs. Also, the syntactic gap is already closing with C#. A growing number of scientific libraries exist for Java and .NET.Personally I tend towards C#, bacause it provides better syntax for multidimensional arrays and somehow feels more 'modern'. But of course, this is only my personal experience.

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