I need to write a plugin for an application that expands with Python 2.7. It should execute a rather complicated dynamic algorithm that works on a rectangular matrix of integers.
The default Python installation that comes with this application does not include a number library such as numpy , so unfortunately I have to implement this using only Python stdlib .
I tried several different approaches for representing a matrix in memory:
values = defaultdict(int) values = [[0 for _ in range(width)] for _ in range(height)] values = [0] * (width * height)
The dictation approach is used only for completeness, in fact it is not very effective, because each element is accessed.
Judging by my measurements, the last one seems to be the fastest to create and access. However, I am surprised that there is no built-in matrix functionality. From what I have learned about Python so far, if you do not find any obvious functionality in stdlib , the most likely reason is that you did not look hard enough.
So I wonder if this can be optimized even further. For example, using the array module or another function that I do not know about.
source share