IronPython vs Python vs for 循环的 C# 性能

IronPython vs Python vs C# performance of for loops

我对 Python 和 IronPython 比较陌生。我对我写的一个相对简单的代码的性能(我应该说慢)感到惊讶:

matrix_column_count=1000
matrix_row_count=2000
matrix=[[random.uniform(0,100) for i in range(matrix_column_count)] for j in range(matrix_row_count)]

运行 IronPython 2.7.10 需要大约 1 秒(有一些小的变化)! 运行 在 Python 3.8.6 上需要 0.55 秒 运行 C# 中类似的东西需要 0.03 秒!

Python 开发与 C# 开发相比,这些数字“正常”吗?还是我做错了什么?

是的,这些数字是正确的。由于我无法发表评论(堆栈溢出点不足),所以我将从堆栈溢出

复制粘贴另一个类似的答案

The answer is simply that Python deals with objects for everything and that it doesn't have JIT by default. So rather than being very efficient by modifying a few bytes on the stack and optimizing the hot parts of the code (i.e., the iteration) – Python chugs along with rich objects representing numbers and no on-the-fly optimizations.

If you tried this in a variant of Python that has JIT (for example, PyPy) I guarantee you that you'll see a massive difference.

A general tip is to avoid standard Python for very computationally expensive operations (especially if this is for a backend serving requests from multiple clients). Java, C#, JavaScript, etc. with JIT are incomparably more efficient.

在这里查看详细答案:

此外,您可能想查看 Python 基准测试与其他编程语言的详细比较,所以这里是链接,可能对您有用:

https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/python.html

https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/csharp.html