Implementing a matrix, which is more efficient - using an Array of Arrays (2D) or a 1D array?

前端 未结 6 934
难免孤独
难免孤独 2020-12-10 14:06

When implementing a Matrix construct using arrays, which would be more efficient? Using a 1D array, or an array of arrays (2D)?

I would think a 2D is more efficient

6条回答
  •  情深已故
    2020-12-10 14:41

    In the general case, the most efficient implementation for any algorithm is the one which has the least amount of code. This is for many reasons:

    • Less code -> less time to write it
    • Fewer lines of code means less bugs (since bugs per KLOC is pretty constant for a given programmer) which are easier to find (since they can't hide well in a few lines of code)
    • If your algorithm isn't fit for the task, it's easier to replace when it's 10 lines instead of 100
    • A compact implementation usually leads to clean interface which makes the code which uses the library clean (so the effect multiplies). This can also lead to a more complex library implementation if that makes the client code more efficient (i.e. you concentrate the effort in a single place to make everything else more simple).

    It also depends a lot on the access patterns. Do you always walk the whole matrix? Is it sparse? Do you prefer to process rows or columns?

    In the extreme case (matrix with a billion rows and columns with only 10 cells used), a HashMap can be more efficient than any array implementation. For other problems, it can be more efficient to mix the approaches depending on the problem (for example, a HashMap of arrays of mini-matrices when cells "clump" in a gigantic empty space).

    If your problems asks to locate a row/column and then process those values, it might be more efficient to use a 2D approach, so the first access returns an array which you can then process without bothering about boundaries, one-off-errors, etc.

提交回复
热议问题