TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Why does changing 0.1f to 0 slow down performance by 10x?

114 点作者 Arkid大约 13 年前

8 条评论

imurray大约 13 年前
I've been hit by nasty slow-downs with denormal numbers a couple of times. It's <i>much</i> easier to hit these problems with single-precision floating point, where near underflow happens much more quickly than with doubles.<p>As a folk-theorem of numerics goes though: such problems are often a symptom that you're not doing quite the right thing. Should your numbers <i>really</i> be that small? Often recasting the problem slightly avoids having to turn on low level flags or other hacking.
评论 #3699582 未加载
评论 #3691780 未加载
jlarocco大约 13 年前
Exact same thing 4 weeks ago: <a href="http://news.ycombinator.com/item?id=3602388" rel="nofollow">http://news.ycombinator.com/item?id=3602388</a>
droithomme大约 13 年前
That code also serves as a good example of how assumptions about a theoretical "optimizing compiler" can lead people into trouble.<p>Something like:<p><pre><code> y[i] = y[i] + 0; </code></pre> "Should" not take more time than:<p><pre><code> y[i] = y[i] + 0.1; </code></pre> because "any decent optimizing compiler obviously would NOP the entire statement out".<p>I seldom see it is the case though where compilers optimize things that coders assume it is obvious the compiler will optimize.
评论 #3690970 未加载
评论 #3691750 未加载
malingo大约 13 年前
This is a good opportunity for me to re-read <a href="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html" rel="nofollow">http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.ht...</a>
sharkbot大约 13 年前
Interesting discussion. Definitely something for the Lua programmers to keep in mind, as the default number implementation type is a C double. I did a quick test on my ancient MacBook, and the translation of the C++ code into Lua showed a slowdown for the denormalized case, although not as dramatic as the original problem (note: I changed the iteration count from 9,000,000 to 900,000 because the original count took way too long).
watmough大约 13 年前
Wow, I wonder how many mysterious performance problems can be traced to things like this?
评论 #3696478 未加载
yread大约 13 年前
Yes, floating point is scary. I always try to stick with integers/fixed points if at all possible. Especially, if there is a loop with addition. That can eat 1 bit of precision per iteration if you're unlucky
pyre大约 13 年前
I'm curious why no one has a good answer for the comments questioning why the compiler doesn't optimize out the +0/-0 instead of converting it to a floating-point, which triggers this issue.
评论 #3690721 未加载
评论 #3690676 未加载
评论 #3690674 未加载