开发者

Math programming optimization [closed]

Closed. This question is off-topic. It is not currently accepting answers.

Want 开发者_运维技巧to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 11 years ago.

Improve this question

I would like to compile a list of tips and tricks on mathematical programming optimization, often I read in the forums things like:

  1. Compare distance using the distance square value because square root calculation is more expensive
  2. (variable * 0.5) is faster than (variable / 2.0)

For me being an programming enthusiast I would like to do my best in wath optimization is concern, Any contribution would be much appreciated


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜