C++ 中的矩阵库与 For 循环

发布于 2024-11-02 02:00:00 字数 323 浏览 3 评论 0原文

您能告诉我使用矩阵库是否会比常规 for 循环产生更快的运行时间吗?目前,我有一些使用 for 循环迭代多维向量来计算矩阵乘积和元素乘积的方法,其中矩阵大小大约为 1000 列 x 400 行。这个方法是我的程序中调用最多的方法,我想知道使用矩阵库是否会提高程序的速度。另外,您会推荐哪个库(来自 http://eigen.tuxfamily.org/index.html php?title=Benchmark,Eigen 对我来说似乎最好)?

谢谢

Could you tell me if using a matrix library results in a faster run-time than regular for-loops? Currently, I have some methods that use for-loops that iterate through multidimensional vectors to calculate matrix products and element-wise products, where the matrix size is roughly 1000 columns by 400 rows. This method is the most called method in my program and I would like to know if using a matrix library would increase the program's speed. Also, which library would you recommend (from http://eigen.tuxfamily.org/index.php?title=Benchmark, Eigen seems best to me)?

Thank You

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

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

发布评论

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

评论(2

梦里的微风 2024-11-09 02:00:00

是的——相当多的 C++ 矩阵库(例如,MTL、uBLAS、Blitz++)使用模板元编程来优化其行为。如果没有理由这样做,我会从 增强uBlas。您可能还想查看 OO numeric 库列表以了解其他可能性。

Yes -- a fair number of C++ matrix libraries (E.g., MTL, uBLAS, Blitz++) use template metaprogramming to optimize their behavior. Absent a reason to do otherwise, I'd start with Boost uBlas. You might also want to look at the OO numerics libraries list for other possibilities.

山田美奈子 2024-11-09 02:00:00

我试图回答“我应该”而不是“哪个”的问题,因为尚不清楚您是否确实需要这样一个库。

矩阵库会缩短执行时间吗?大概。他们在高中教给你的方法肯定不是最快的。然而,还有其他问题需要考虑。

首先,你是否过早地进行优化?试图让你的程序尽可能快地运行是很诱人的,但并不总是正确的做法。您必须确定这样做是否真的是一种有效的消磨时间的方式。

其次,速度会对可用性产生显着影响吗?让一个程序在 2 秒而不是 4 秒内运行确实不值得……但是 30 小时而不是 60 小时呢?也许是这样。我喜欢强调在进行抛光之前让一切正常运转。

最后,我遇到了几个别人几年前编写的代码示例,这些代码完全没有用。无法使用新操作系统或编译器或其他东西找到或编译的旧库意味着我必须完全重写一些东西,浪费了我几周的时间。最初,获得额外百分之几的性能似乎是个好主意,但这意味着他们的代码的生命周期有限,特别是因为文档很差。

保持简单愚蠢对于很多事情来说都是一个很好的口头禅。我强烈主张仅在绝对必要时才使用库,然后仅使用那些看起来寿命长且稳定的库。

I am trying to answer the question "should I" instead of "which one" because it isn't clear that you actually need such a library.

Would a matrix library improve execution time? Probably. The methods they teach you in high school are certainly not the fastest. However there are other issues to consider.

First, are you optimizing prematurely? Trying to make your program as fast as possible as soon as possible is tempting, but not always the right thing to do. You have to make the determination if doing so is really a valid way to spend your time.

Second, will speed have any significant effect on usability? Making a program work in 2 seconds instead of 4 seconds isn't really worth the effort.... but 30 hours instead of 60 hours? Maybe so. I like to put emphasis on getting everything working before doing the polishing.

Finally, I have met several examples of code somebody else wrote several years before which was utterly useless. Old libraries that couldn't be found or compiled with a new OS or compiler or something different meant that I had to completely rewrite something wasting weeks of my time. It may have seemed like a good idea originally to get that extra few percent performance, but it meant that their code had a limited life span, especially because of poor documentation.

Keep It Simple Stupid is an excellent mantra for so many things. I am a strong advocate for only using libraries when absolutely necessary, and then only using those which seem to be long lived and stable.

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