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.
我想说,首先要坚持良好的开发实践,阅读 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.
发布评论
评论(2)
两个关键点。
Two key points.
我想说,在考虑优化之前首先要确定的是库的范围和预期目的。例如,这个库是 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