在STL向量中存储矩阵信息。向量或向量的向量哪个更好?

发布于 2024-11-16 09:12:29 字数 234 浏览 1 评论 0原文

我创建了自己的 Matrix 类,在类内部,有关 Matrix 的信息存储在 STL 向量中。我注意到,在搜​​索网络时,有些人使用向量的向量来表示矩阵信息。我最好的猜测告诉我,只要矩阵很小或很瘦(row_num>>column_num),差异就应该很小,但是如果矩阵是方形或胖矩阵(row_num<<column_num)呢?如果我要创建一个非常大的矩阵,我会看到运行时间的差异吗?还有其他因素需要考虑吗?

谢谢

I've created my own Matrix class were inside the class the information regarding the Matrix is stored in a STL vector. I've notice that while searching the web some people work with a vector of vectors to represent the Matrix information. My best guess tells me that so long as the matrix is small or skinny (row_num >> column_num) the different should be small, but what about if the matrix is square or fat (row_num << column_num)? If I were to create a very large matrix would I see a difference a run time? Are there other factors that need to be considered?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亚希 2024-11-23 09:12:29

您是否考虑过使用现成的矩阵表示(例如 boost)而不是重新发明轮子?

例如,如果您有很多空行,则使用嵌套表示可以节省大量空间。除非您在实际用例中有具体信息表明一种方法不能满足您的要求,否则请采用最容易维护和正确实施的方式进行编码。

Have you considered using an off-the-shelf matrix representation such as boost's instead of reinventing the wheel?

If you have a lot of empty rows for example, using the nested representation could save a lot of space. Unless you have specific information in actual use cases showing one way isn't meeting your requirements, code the way that's easiest to maintain and implement properly.

静水深流 2024-11-23 09:12:29

有太多变量无法回答你的问题。

创建一个抽象,以便您的代码不关心矩阵的表示方式。然后使用任何实现编写代码。然后分析它。

如果你的矩阵很密集,“向量的向量”不太可能比单个大内存块更快,而且可能会更慢。 (追逐两个指针进行随机访问+更糟糕的局部性。)

如果您的矩阵很大且稀疏,那么您问题的正确答案可能是“两者都不是”。

因此,创建一个抽象接口,编写一些代码,然后对其进行分析。 (正如 @Mark 所说,您可能应该考虑很多第三方库。)

There are too many variables to answer your question.

Create an abstraction so that your code does not care how the matrix is represented. Then write your code using any implementation. Then profile it.

If your matrix is dense, the "vector of vectors" is very unlikely to be faster than a single big memory block and could be slower. (Chasing two pointers for random access + worse locality.)

If your matrices are large and sparse, the right answer to your question is probably "neither".

So create an abstract interface, code something up, and profile it. (And as @Mark says, there are lots of third-party libraries you should probably consider.)

手心的温暖 2024-11-23 09:12:29

如果将所有内容存储在单个向量中,迭代器将遍历整个矩阵。如果使用向量的向量,迭代器将仅遍历单个维度。

If you store everything in a single vector, an iterator will traverse the entire matrix. If you use a vector of vectors, an iterator will only traverse a single dimension.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文