I am trying to work with large 2D arrays in Python, but it's very slow. For example:
start = time.time()
result = numpy.empty([5000, 5000])
for i in range(5000):
for j in range(5000):
result[i, j] = (i * j) % 10
end = time.time()
print(end - start) # 8.8 s
Same program in Java is much faster:
long start = System.currentTimeMillis();
int[][] result = new int[5000][5000];
for (int i = 0; i < 5000; i++) {
for (int j = 0; j < 5000; j++) {
result[i][j] = (i * j) % 10;
}
}
long end = System.currentTimeMillis();
System.out.println(end - start); // 121 ms
It's because Python is interpreted language? Is there any way to improve it? Or why Python is so popular for working with matrices, artificial intelligence, etc.?