nsiatras / programming-languages-benchmark

A performance comparison for various programming languages
0 stars 1 forks source link

Pure python version #1

Closed torriem closed 2 years ago

torriem commented 2 years ago

Just a note, but not really an issue.

The problem with most benchmarks of this type is they tend to be transliterations from one language to another rather than a realistic, idiomatic, implementation in the target language. Here's a pure python version that is much more realistic and represents python code you'd see in the real world. `import random import time

n = 1024

def matrix_mul(a, b): return [[sum(i j for i, j in zip(r, c)) for c in zip(b)] for r in a]

Add random values to Arrays A and B

A = [[random.random() for row in range(n)] for col in range(n)] B = [[random.random() for row in range(n)] for col in range(n)]

print("Python Matrix Multiplication Benchmark started. Please wait...");

start = time.time()

Multiply A to B values and add them to C

C = matrix_mul(A,B)

end = time.time() print("Python Matrix Multiplication Benchmark finished in: %0.6f" % (end-start))`

This version runs only 4 times slower than your FreeBASIC code. This is a much more realistic demonstration of Python's performance when using pure python code (not using numpy).

nsiatras commented 2 years ago

@torriem Hello,

Best Solutions

The best solution to the problem my benchmark describes is linear algebra. The second best is Java's parallel streams. And the third is numpy which implements a pure linear algebra solution.

About the benchmark

With this benchmark I am not comparing features or how clever a developer can be. I am comparing how fast a programming language can do a for loop, the most basic loop in the history of programming, and add data to an array.

Perhaps the C = matrix_mul(A,B) is much faster than 3 for loops but you cannot use matrix_mul all the time. For example on a stockmarket webservice reading data from a client, multiplying 2 values included in the data with a 3rd value included in the service and sending them back. These multiplications are independent and cannot be done with matrix_mul(). In this case the same server software written in Python would take 4426 seconds/processor core to serve its clients while a server written in FreeBasic would take just 25.8/processor core and I am not even going to add Java to this battle.

Conclusion

Python is a horrible programming language. It has a horrible syntax and it is horribly slow.