How raise to power works? Is it worth to use pow(x, 2)?

Is it more efficient to do multiplication than raise to power 2 in c++? I am trying to do final detailed optimizations. Will the compiler treat x*x the same as pow(x,2)? If I remember correctly, multiplication was better for some reason, but maybe it does not matter in c++11. Thanks 如果你将乘法与pow()标准库函数进行比较,那么是的,乘法肯定会更快。 I general, you should not worry about pic

如何提高电力工程? 是否值得使用pow(x,2)?

在c ++中进行乘法比提高功率2效率更高? 我正在尝试做最后的详细优化。 编译器会将x * x与pow(x,2)相同吗? 如果我没有记错,乘法因为某种原因更好,但是在c ++ 11中可能并不重要。 谢谢 如果你将乘法与pow()标准库函数进行比较,那么是的,乘法肯定会更快。 总的来说,除非您有证据表明存在热点(即,除非您已在实际情况下对您的代码进行了剖析并确定了特定的代码块,否则您不应该担心这种微型优化。您的聪明窍门实

Floating point division vs floating point multiplication

Is there any (non-microoptimization) performance gain by coding float f1 = 200f / 2 in comparision to float f2 = 200f * 0.5 A professor of mine told me a few years ago that floating point divisions were slower than floating point multiplications without elaborating the why. Does this statement hold for modern PC architecture? Update1 In respect to a comment, please do also consider this

浮点除法与浮点乘法

编码是否有任何(非微型优化)性能增益? float f1 = 200f / 2 与...相比 float f2 = 200f * 0.5 几年前,我的一位教授告诉我,浮点数的划分比浮点乘法要慢,但没有详细说明原因。 这种说法是否适用于现代PC架构? UPDATE1 关于评论,请考虑这种情况: float f1; float f2 = 2 float f3 = 3; for( i =0 ; i < 1e8; i++) { f1 = (i * f2 + i / f3) * 0.5; //or divide by 2.0f, respectively } 更新2从评论中引

Floating point comparison revisited

This topic has come up many times on StackOverflow, but I believe this is a new take. Yes, I have read Bruce Dawson's articles and What Every Computer Scientist Should Know About Floating-Point Arithmetic and this nice answer. As I understand it, on a typical system there are four basic problems when comparing floating-point numbers for equality: Floating point calculations are not exact

浮点比较重新审视

这个话题在StackOverflow上多次出现,但我相信这是一个新的观点。 是的,我已阅读Bruce Dawson的文章以及每位计算机科学家应该了解的浮点算术和这个很好的答案。 据我了解,在一个典型的系统中,当比较浮点数时,存在四个基本问题: 浮点计算不准确 ab是否“小”取决于a和b ab是否“小”取决于a和b的类型(例如float,double,long double) 浮点通常具有+ -infinity,NaN和非规范化表示,其中任何一个都可能会干扰一个天

NaN == 0.0 with the Intel C++ Compiler?

It is well-known that NaNs propagate in arithmetic, but I couldn't find any demonstrations, so I wrote a small test: #include <limits> #include <cstdio> int main(int argc, char* argv[]) { float qNaN = std::numeric_limits<float>::quiet_NaN(); float neg = -qNaN; float sub1 = 6.0f - qNaN; float sub2 = qNaN - 6.0f; float sub3 = qNaN - qNaN; float add

NaN == 0.0与英特尔C ++编译器?

众所周知,NaNs在算术中传播,但我找不到任何示范,所以我写了一个小测试: #include <limits> #include <cstdio> int main(int argc, char* argv[]) { float qNaN = std::numeric_limits<float>::quiet_NaN(); float neg = -qNaN; float sub1 = 6.0f - qNaN; float sub2 = qNaN - 6.0f; float sub3 = qNaN - qNaN; float add1 = 6.0f + qNaN; float add2 = qNaN + qNaN;

Denormalized floating point in Objective

What is the relevance of Stack Overflow question/answer Why does changing 0.1f to 0 slow down performance by 10x? for Objective-C? If there is any relevance, how should this change my coding habits? Is there some way to shut off denormalized floating points on Mac OS X? It seems like this is completely irrelevant to iOS. Is that correct? As I said in response to your comment there: it i

Objective中的非规格化浮点

Stack Overflow的相关性问题/答案为什么将0.1f更改为0会使性能下降10倍? 为Objective-C? 如果有任何相关性,这应如何改变我的编码习惯? 有什么方法可以关闭Mac OS X上的非规格化浮点? 这似乎与iOS完全无关。 那是对的吗? 正如我在回答你的评论时说的那样: 它不仅仅是一个CPU而是一个语言问题,所以它可能与x86上的Objective-C有关。 (iPhone的ARMv7似乎不支持非规范化浮点数,至少使用默认的运行时/构建设置)

What is a good CPU/PC setup to speed up intensive C++/templates compilation?

I currently have a machine with an Opteron 275 (2.2Ghz), which is a dual core CPU, and 4GB of RAM, along with a very fast hard drive. I find that when compiling even somewhat simple projects that use C++ templates (think boost, etc.), my compile times can take quite a while (minutes for small things, much longer for bigger projects). Unfortunately only one of the cores is pegged at 100%, so I k

什么是一个好的CPU / PC设置来加速密集的C ++ /模板编译?

我目前有一台带有Opteron 275(2.2Ghz)的机器,这是一款双核CPU,4GB内存以及非常快速的硬盘。 我发现在编译使用C ++模板的很简单的项目(想一想boost等)时,我的编译时间可能需要相当长的一段时间(小事情需要几分钟,较大项目需要很长时间)。 不幸的是,只有一个内核被锁定在100%,所以我知道这不是I / O,似乎没有办法利用C ++编译的其他内核。 使用模板编译时间问题通常是链接问题,而不是编译问题。 在.cpp文件内

Which features of C++ are particularly resource intensive at compile time?

I believe C is generally faster to compile than C++, because it lacks features like late binding and operator overloading. I'm interested in knowing which features of C++ tend to slow the compilation process the most? This is a difficult question to answer in a meaningful way. If you look purely at lines of code per second (or something on that order), there's no question that a C com

C ++的哪些特性在编译时特别占用资源?

我相信C编译通常比C ++更快,因为它缺少后期绑定和运算符重载等功能。 我有兴趣知道C ++的哪些功能倾向于最慢地减慢编译过程? 这是一个很难回答的问题。 如果你纯粹看每秒代码行(或者按顺序),毫无疑问C编译器应该比C ++编译器更快。 就其本身而言,这并不意味着很多。 在这个问题中提到后期绑定就是一个很好的例子:编译C ++虚拟函数的速度至少比编译C(非虚拟)函数要慢一些。 这并不意味着什么 - 这两者根本不相同

How do I find out why g++ takes a very long time on a particular file?

I am building a lot of auto-generated code, including one particularly large file (~15K lines), using a mingw32 cross compiler on linux. Most files are extremely quick, but this one large file takes an unexpectedly long time (~15 minutes) to compile. I have tried manipulating various optimization flags to see if they had any effect, without any luck. What I really need is some way of determin

我如何知道为什么g ++在特定文件上花费很长时间?

我在linux上使用mingw32交叉编译器构建了很多自动生成的代码,包括一个特别大的文件(约15K行)。 大多数文件非常快,但是这个大文件需要很长时间(约15分钟)才能编译。 我尝试过操纵各种优化标志,看看它们是否有任何效果,没有任何运气。 我真正需要的是确定g ++在做什么的一些方法,这需要花费很长时间。 是否有任何(相对简单的)方法让g ++产生关于不同编译阶段的输出,以帮助我缩小可能的挂断? 可悲的是,我没有

C++: Where to write the code documentation: in .cpp or in .hpp files?

Where is it customary to write the in-code documentation of classes and methods? Do you write such doc-blocks above the corresponding class/method in the header (.hpp) file, or within the source (.cpp) file? Is there a widely respected convention for such things? Do most C++ projects do it one way rather than the other? Or should documentation be written on the two sides (ie in the .hpp an

C ++:在哪里编写代码文档:在.cpp或.hpp文件中?

在哪里编写类和方法的代码文档习惯? 您是否在头文件(.hpp)文件或源文件(.cpp)内的相应类/方法之上编写了这样的文档块? 有这样一个广受尊敬的公约吗? 大多数C ++项目是以一种方式而不是另一种方式进行的? 还是应该在双方(即在.hpp和.cpp文件中)编写文档,可能只有一个简短的描述,另一个较长的描述? 最重要的是,是否有任何实际考虑因素使得以单向而非其他方式写作更方便? (例如使用自动文档解析器和Doxyg

#include all .cpp files into a single compilation unit?

I recently had cause to work with some Visual Studio C++ projects with the usual Debug and Release configurations, but also 'Release All' and 'Debug All', which I had never seen before. It turns out the author of the projects has a single ALL.cpp which #includes all other .cpp files. The *All configurations just build this one ALL.cpp file. It is of course excluded from the re

#include所有.cpp文件到一个编译单元中?

我最近有理由使用通常的调试和发布配置与一些Visual Studio C ++项目一起工作,但也有'全部释放'和'全部调试',这是我以前从未见过的。 事实证明,项目的作者有一个ALL.cpp,其中包含所有其他.cpp文件。 *所有配置只是建立这一个ALL.cpp文件。 它当然会排除在常规配置之外,并且常规配置不会构建ALL.cpp 我只是想知道这是否是一种常见做法? 它带来了什么好处? (我的第一反应是它闻起来很糟糕。)