我一直在挖掘Linux内核的某些部分,发现这样的调用:
if (unlikely(fd < 0))
{
/* Do something */
}
Run Code Online (Sandbox Code Playgroud)
要么
if (likely(!err))
{
/* Do something */
}
Run Code Online (Sandbox Code Playgroud)
我找到了它们的定义:
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
Run Code Online (Sandbox Code Playgroud)
我知道它们是为了优化,但它们是如何工作的?使用它们可以预期性能/尺寸减少多少?至少在瓶颈代码中(当然在用户空间中)是否值得麻烦(并且可能失去可移植性).
我这里有一个"Schroedinger's Cat"类型的问题 - 我的程序(实际上是我的程序的测试套件,但是程序仍然是)崩溃,但只有在发布模式下构建时才会发生,并且只有在从命令行启动时.通过穴居人调试(即整个地方讨厌的printf()消息),我已经确定了代码崩溃的测试方法,但遗憾的是实际的崩溃似乎发生在某些析构函数中,因为我看到的最后一条跟踪消息都在其他执行干净的析构函数.
当我尝试在Visual Studio中运行此程序时,它不会崩溃.从WinDbg.exe启动时也是如此.仅从命令行启动时才会发生崩溃.这是在Windows Vista,btw下发生的,不幸的是我现在无法访问XP机器进行测试.
这将是非常好的,如果我能得到的Windows打印出堆栈跟踪,或一些其他不是简单地结束,如果它已经退出干净方案.有没有人对如何在这里获得更有意义的信息有任何建议,希望能解决这个问题?
编辑:问题确实是由一个越界数组引起的,我在这篇文章中对此进行了更多描述.感谢大家帮忙找到这个问题!
我刚刚开始使用Linux上的g ++编译器,并对编译器标志有一些疑问.他们是这样的
优化
我阅读了有关优化标志的内容-O1,-O2并-O3在g ++手册页中.我不明白何时使用这些标志.通常你使用什么优化级别?g ++手册说明了以下内容-O2.
优化甚至更多.GCC几乎执行所有支持的优化,不涉及空速 - 权衡.指定-O2时,编译器不执行循环展开或函数内联.与-O相比,此选项增加了编译时间和生成代码的性能.
如果它没有进行内联和循环展开,那么如何实现所述性能,是否建议使用此选项?
静态库
如何使用g ++创建静态库?在Visual Studio中,我可以选择一个类库项目,它将被编译成"lib"文件.什么是g ++中的等价物?
javac 有一个有趣的-O选择:
通过内联静态,最终和私有方法来优化编译代码.请注意,您的课程可能会变得更大.
这个选项似乎不受欢迎(隐藏?),我今天刚刚在CodeCup 2014页面上发现它.
-O中未提及的官方文件,也不在man javac......奇怪.
在对类似问题的接受答案中,我们可以读到:
Java中的优化主要由JIT编译器在运行时完成.因此,没有必要尝试指示它在编译时优化某种方式(无论如何它只创建字节码).JIT几乎肯定会在现场做出更好的决策,了解确切的环境并观察代码特定部分的实际执行模式.
我的问题是:
我应该总是使用该-O选项吗?换句话说,代码总是运行得更快-O或根本没有差别?
也许班级规模会增加太多,整体表现会下降?或者JVM无论如何都会进行内联,所以最好留下它?
类似的故事是与gcc -O3国旗.
考虑以下程序.
#include <stdio.h>
int negative(int A) {
return (A & 0x80000000) != 0;
}
int divide(int A, int B) {
printf("A = %d\n", A);
printf("negative(A) = %d\n", negative(A));
if (negative(A)) {
A = ~A + 1;
printf("A = %d\n", A);
printf("negative(A) = %d\n", negative(A));
}
if (A < B) return 0;
return 1;
}
int main(){
divide(-2147483648, -1);
}
Run Code Online (Sandbox Code Playgroud)
在没有编译器优化的情况下编译它时,它会产生预期的结果.
gcc -Wall -Werror -g -o TestNegative TestNegative.c
./TestNegative
A = -2147483648
negative(A) = 1
A = -2147483648
negative(A) = 1 …Run Code Online (Sandbox Code Playgroud) 我对GCC -O3标志非常熟悉,但是它与-Os有何不同,在哪种情况下我们应该优先选择其他?
我发现这个主题为什么处理排序数组比未排序数组更快?.并尝试运行此代码.而且我发现了奇怪的行为.如果我使用-O3优化标志编译此代码,则需要2.98605 sec运行.如果我用-O2它编译1.98093 sec.我尝试在同一环境中的同一台机器上运行此代码几次(5或6),我关闭所有其他软件(chrome,skype等).
gcc --version
gcc (Ubuntu 4.9.2-0ubuntu1~14.04) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)
那么请你能解释一下为什么会这样吗?我阅读gcc手册,我看到-O3包括-O2.谢谢你的帮助.
PS添加代码
#include <algorithm>
#include <ctime>
#include <iostream>
int main()
{
// Generate data
const unsigned arraySize = 32768;
int data[arraySize];
for (unsigned …Run Code Online (Sandbox Code Playgroud) 我有一个MCVE,在我的一些机器上崩溃时使用g ++版本4.4.7编译但是与clang ++版本3.4.2和g ++版本6.3一起使用.
我想要一些帮助来知道它是来自未定义的行为还是来自这个古老版本的gcc的实际错误.
#include <cstdlib>
class BaseType
{
public:
BaseType() : _present( false ) {}
virtual ~BaseType() {}
virtual void clear() {}
virtual void setString(const char* value, const char* fieldName)
{
_present = (*value != '\0');
}
protected:
virtual void setStrNoCheck(const char* value) = 0;
protected:
bool _present;
};
// ----------------------------------------------------------------------------------
class TypeTextFix : public BaseType
{
public:
virtual void clear() {}
virtual void setString(const char* value, const char* fieldName)
{
clear();
BaseType::setString(value, fieldName);
if( _present == …Run Code Online (Sandbox Code Playgroud) 关于编译器(GCC)所做的优化,标准做法是什么?每个选项(-O,-O1,-O2,-O3,-Os,-s,-fexpensive-optimizations)的作用有何不同,我如何确定最佳选项?
我正在编译程序:
#include <iostream>
#include <vector>
#include <cstddef>
#include <algorithm>
struct Model
{
int open, extend;
};
struct Cell
{
int a, b;
};
typedef std::vector<std::vector<Cell>> DPMatrix;
void print(const DPMatrix& matrix)
{
for (std::size_t i = 0; i < matrix.size(); ++i) {
for (std::size_t j = 0; j < matrix[i].size(); ++j) {
std::cout << '{' << matrix[i][j].a << ' ' << matrix[i][j].b << "} ";
}
std::cout << std::endl;
}
}
DPMatrix init_dp_matrix(const std::size_t num_cols, const std::size_t num_rows, const Model& model) …Run Code Online (Sandbox Code Playgroud) 作为一个新手,我试图找出使用GCC优化标志时会发生什么.哪些优化标志可能产生副作用.我应该避免使用哪种特殊情况进行优化.我想优化可以打破一些多线程代码,但还有什么?
我试图在我的 Ubuntu 18.04 系统上编译https://github.com/fogleman/HelloGL中的简约 C opengl 代码,但出现以下错误:
\ngcc -c -o build/matrix.o -std=c99 -O3 src/matrix.c \ngcc -o main build/shader.o build/main.o build/util.o build/matrix.o -lglew -lglfw3 -framework Cocoa \n-framework OpenGL -framework IOKit -framework CoreVideo -lm \ngcc: error: Cocoa: No such file or directory \ngcc: error: OpenGL: No such file or directory \ngcc: error: IOKit: No such file or directory \ngcc: error: CoreVideo: No such file or directory \ngcc: error: unrecognized command line option \xe2\x80\x98-framework\xe2\x80\x99 \ngcc: error: unrecognized command line option …Run Code Online (Sandbox Code Playgroud)