- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在处理演示各种 C++ 优化的演示文稿,并坚持使用 const
的示例允许他们。
考虑以下代码:
object.h
class Object {
int i1;
int i2;
public:
Object(int i1_, int i2_) : i1(i1_), i2(i2_) {}
int getI1() const { return i1; }
int getI2() const { return i2; }
std::pair<int, int> calculate() const {
return std::pair<int, int>(i1 + i2, i1 * i2);
}
};
constopt.cpp
#include <iostream>
#include "object.h"
int main() {
Object o(10, 20);
std::cout << o.getI1() << " + " << o.getI2() << " = "
<< o.calculate().first << std::endl
<< o.getI1() << " * " << o.getI2() << " = "
<< o.calculate().second << std::endl;
return 0;
}
当 calculate()
是内联的,一切正常,G++ 直接将常量(10 和 20)传递给 operator <<
缓存 getI1()
和 getI2()
调用:
mov $0xa,%esi
mov $0x601080,%edi
callq 0x400740 <_ZNSolsEi@plt>
但是当我移动calculate()
到一个单独的翻译单元,它强制i1
和 i2
获取两次(在 o.calculate().first
之前和之后):
mov (%rsp),%esi
mov 0x4(%rsp),%r14d
mov $0x601080,%edi
callq 0x400740 <_ZNSolsEi@plt>
我看不出有什么不同,因为getI1()
不依赖于 calculate()
可能产生的任何副作用和 Object
即使在 calculate()
时也是 const在单独的翻译单元中。
是 G++ 不够聪明还是没有资格在这种情况下执行优化?我的猜测是它可以缓存getI1()
和 getI2()
电话来自那个答案:How does const after a function optimize the program?
我使用 gcc 4.8.1 版。 -Os 和 -O2 我都试过了。
似乎const
在这种情况下,不在 GCC 优化器中使用。相反,它会自己挖掘(不能在不同的翻译单元中执行),并搜索 pure and const functions .函数可以手动标记为 __attribute__((const))
.消除额外的阶段 getI*()
调用称为 FRE(完全冗余消除)。
最佳答案
好吧,要准确回答您的问题,必须了解编译器的实现细节。我不知道,所以接下来的内容纯属推测。
Object::i1
和 Object::i2
未声明为常量。因此,编译器在没有看到 Object::calculate
的定义的情况下,不得不假定它们可能会发生变化。
Object::calculate
本身是 const 本身并不排除它对 Object::i1
和 Object::i2
进行更改.考虑以下稍作修改的示例:
class Object {
int i1;
int i2;
int* p1;
public:
Object(int i1_, int i2_) : i1(i1_), i2(i2_), p1(&i1) {}
int getI1() const { return i1; }
int getI2() const { return i2; }
std::pair<int, int> calculate() const;
};
std::pair<int, int> Object::calculate() const {
(*p1)++;
return std::pair<int, int>(i1 + i2, i1 * i2);
}
此外,o
一开始就没有声明为 const,因此 Object::calculate
有权做一些像 const_cast
这样粗鲁的事情并摆脱它!
关于c++ - G++ : Moving to another translation unit breaks "const optimization"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29419563/
我正在尝试运行以下代码片段,以使曲线适合一些经验数据,但在Julia Optim.jl包中,optimize()方法一直存在问题。我正在使用Julia v1.1.0,并安装了所有正确的软件包。我不断收
时不时你会听到一些故事,这些故事旨在说明某人在某件事上有多擅长,有时你会听到这个人如何热衷于代码优化,以至于他优化了他的延迟循环。 因为这听起来确实是一件奇怪的事情,因为启动“计时器中断”而不是优化的
我正在尝试使用 z3py 作为优化求解器来最大化从一张纸上切出的长方体的体积。 python API 提供了 Optimize() 对象,但使用它似乎不可靠,给我的解决方案显然不准确。 我尝试使用 h
我今天接受了采访。这个问题是为了优化下面的代码。如果我们将在 for 循环之后看到下面的代码,那么下面有四个“if-else”步骤。所以,面试官要求我将其优化为 3 if-else 行。我已经尝试了很
我使用BFGS算法使用Optim.jl库来最小化Julia中的函数。今天,我问了一个关于同一个库的question,但是为了避免混淆,我决定将它分成两部分。 我还想对优化后的负逆黑森州进行估算,以进行
在 haskell 平台中实现许多功能时有一个非常常见的模式让我很困扰,但我找不到解释。这是关于使用嵌套函数进行优化。 where 子句中的嵌套函数旨在进行尾递归的原因对我来说非常清楚(如 lengt
我目前正试图利用 Julia 中的 Optim 包来最小化成本函数。成本函数是 L2 正则化逻辑回归的成本函数。其构造如下; using Optim function regularised_cost
我正在使用 GEKKO 来解决非线性规划问题。我的目标是将 GEKKO 性能与替代方案进行比较,因此我想确保我从 GEKKO 中获得其所能提供的最佳性能。 有n个二元变量,每个变量都分配有一个权
我可以手动更改参数C和epsilon以获得优化结果,但我发现有PSO(或任何其他优化算法)对SVM进行参数优化。没有算法。什么意思:PSO如何自动优化SVM参数?我读了几篇关于这个主题的论文,但我仍然
我正在使用 scipy.optimize.fmin_l_bfgs_b 来解决高斯混合问题。混合分布的均值通过回归建模,其权重必须使用 EM 算法进行优化。 sigma_sp_new, func_val
当你有一个 Option ,编译器知道 NULL永远不是 &T 的可能值, 和 encodes the None variant as NULL instead .这样可以节省空间: use std:
当你有一个 Option ,编译器知道 NULL永远不是 &T 的可能值, 和 encodes the None variant as NULL instead .这样可以节省空间: use std:
以下是说明我的问题的独立示例。 using Optim χI = 3 ψI = 0.5 ϕI(z) = z^-ψI λ = 1.0532733 V0 = 0.8522423425 zE = 0.598
根据MySQL文档关于Optimizing Queries With Explain : * ALL: A full table scan is done for each combination o
我无法预览我的 Google 优化工具体验。 Google 优化抛出以下错误: 最佳答案 我也经常遇到这种情况。 Google 给出的建议是错误的。清除 cookie 并重新启动浏览器并不能解决问题。
我一直在尝试使用 optim()或 optimize()函数来最小化绝对预测误差的总和。 我有 2 个向量,每个长度为 28,1 个包含预测数据,另一个包含过去 28 天的实际数据。 fcst和 ac
在我对各种编译器书籍和网站的独立研究中,我了解到编译器可以优化正在编译的代码的许多不同方法,但我很难弄清楚每种优化会带来多少好处给予。 大多数编译器编写者如何决定首先实现哪些优化?或者哪些优化值得付出
我在我的项目中使用 System.Web.Optimizations BundleConfig。我在我的网站上使用的特定 jQuery 插件遇到了问题。如果我将文件添加到我的 ScriptBundle
我收到这个错误 Error: webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.
scipy的optimize.fmin和optimize.leastsq有什么区别?它们似乎在 this example page 中以几乎相同的方式使用.我能看到的唯一区别是 leastsq 实际上
我是一名优秀的程序员,十分优秀!