1

I am relatively new to Python and IronPython. I was suprised about the performance (should I say slowness) of a relatively simple code I wrote:

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)]

Running this on IronPython 2.7.10 takes (with some small variations) about 1 sec! Running on Python 3.8.6 takes 0.55 sec Running something similar in C# takes 0.03 sec !

Are these numbers "normal" for Python development vs C# development or am I doing something wrong?

ohnezahn
  • 453
  • 4
  • 9

1 Answers1

3

Yes, these are numbers are correct. As I cannot comment (not enough stack overflow points), so I will copy paste another similar answer here from stack overflow

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.

Check the detailed answer here: https://stackoverflow.com/a/29904752/13993546

Also, you might want to check detailed comparison of Python benchmarks vs other programming languages, so here are the links, which might be useful for you:

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

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

Jyoti
  • 116
  • 3