数学编程优化

发布于 2024-12-08 09:55:46 字数 1459 浏览 2 评论 0原文

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

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

发布评论

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

评论(2

陌路终见情 2024-12-15 09:55:46

两个关键点。

  • 测量 - 不要假设您知道什么是慢的和/或原因。在实际生产代码中测量它。然后只需要担心那些占用你大部分时间的事情。
  • 优化你的算法而不是你的代码。寻找您正在做的事情是 o(N^2) 而不是 o(N) 或 o(N) 而不是 o(ln(N)) 并切换到具有更好渐近行为的算法。

Two key points.

  • MEASURE - dont assume you know what is slow and/or why. Measure it in real production code. Then only worry about the bit that is chewing up most of your time.
  • Optimise your algorithm not your code. Look for somethig that you're doing that is o(N^2) instead of o(N) or is o(N) instead of o(ln(N)) and switch to an algorithm with better asymptotic behaviour.
不知所踪 2024-12-15 09:55:46

我想说,在考虑优化之前首先要确定的是库的范围和预期目的。例如,这个库是 2D 还是 3D?它是否包含几何算法,例如凸包?

像大多数开发此类库的人一样,您会遇到一些不可避免的问题。像精度误差这样的事情有时肯定会让你发疯。也要小心退化三角形。

仔细考虑包含 epsilon 或容差的算法。这是一个很好的功能,但它会让你的算法变得更加复杂。

如果您涉足 3D 世界,请以不同的方式对待点和矢量(这是 3D 数学中最常见的问题之一)。考虑对模板乘法进行元编程(我感觉这个会受到批评),因为它可以大大加快渲染速度。

一般来说,除了实质性算法之外,尽量避免虚拟调用任何东西,像向量或点这样的小类不应该被继承(另一个火爆的机会)。

我想说,首先要坚持良好的开发实践,阅读 Scott Meyers 的 Efficient C++ 和 More Efficient C++,如果您采取捷径,例如比较平方值以避免平方根计算,请注释您的代码,以便未来的开发人员能够理解数学。

最后,不要尝试预先过度优化,为此使用分析器。就我个人而言,我经常从编写最优雅的解决方案开始(我应该说我认为最优雅的解决方案是什么),然后进行优化,您会惊讶于 C++ 优化器通常做得有多好。

希望这对

马丁有帮助

I would say, the first thing to pin down before thinking of optimisation is the scope and intended purpose of your library. For example, is this library 2D or 3D does it includes geometrical algorithms, like convex hull?

Like most people developing such library you will run into a few unavoidable issues. Things like precisions errors can definitely drive you mad at times. Beware of degenerated triangles as well.

Consider algorithms that include an epsilon or tolerance carefully. This is a neat feature to have, but it will make your algorithms more complex.

If you venture in the world of 3D, treat point and vector differently (this is one of the most common issue in 3D math). Consider meta programming for template multiplications (this one will get flamed I feel it) as it can considerably speed up rendering.

In general, try to avoid virtual calls for anything but substantial algorithms, small classes like vectors or points should not be inherited (another flaming opportunity).

I would say, start by sticking to good development practice, read Efficient C++ and More Efficient C++ by Scott Meyers and If you take short cuts like comparing the squared value to avoid a square root calculation, comment your code so future developer can understand the maths.

Finally, do not try to over optimize up front, use a profiler for this. Personally I often start by coding the most elegant solution (should I say what I consider the most elegant solution) and then optimize, you will be surprised at how good a job the C++ optimizer often do.

Hope this helps

Martin

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