请考虑以下代码:
void error_handling();
bool method_impl();
bool method()
{
const bool res = method_impl();
if (res == false) {
error_handling();
return false;
}
return true;
}
我知道 method_impl() 会返回 true 99.999%(是的,三位小数),但我的编译器不会.method() 在时间消耗方面是部分关键的.
I know method_impl() will return true 99.999% (yes, three decimal places) of the time, but my compiler doesn't. method() is partially critical in term of time-consumption.
method()(并降低可读性)以确保仅在method_impl() 返回false 时才可能发生跳转?如果是,如何?method() (and make it less readable) to ensure a jump may only occur when method_impl() returns false? If yes, how?底层硬件已经执行了这种优化.第一次预测它会失败",但在它会命中正确的选项 en.wikipedia.org/wiki/Branch_predictor 之后.
The underlying hardware already performs this optimizations. It will "fail" to predict it the first times, but after it will hit the correct option en.wikipedia.org/wiki/Branch_predictor.
您可以尝试应用 GCC 扩展并检查使用它是否更快,但我认为您几乎看不出有和没有它的任何区别.总是应用分支预测,它不是你启用的东西
You can try applying the GCC extension and check if it is faster with it or not, but I think you will barely see any difference with it and without it. The branch prediction is applied always, it is not something you enable
这篇关于为已知的更常见路径优化分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
fpermissive 标志有什么作用?What does the fpermissive flag do?(fpermissive 标志有什么作用?)
如何在我不想编辑的第 3 方代码中禁用来自 gccHow do you disable the unused variable warnings coming out of gcc in 3rd party code I do not wish to edit?(如何在我不想编辑的第 3 方代码中禁
使用 GCC 预编译头文件Precompiled headers with GCC(使用 GCC 预编译头文件)
如何在 OS X 中包含 omp.h?How to include omp.h in OS X?(如何在 OS X 中包含 omp.h?)
如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文How can I make GCC compile the .text section as writable in an ELF binary?(如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文件中?)
GCC、字符串化和内联 GLSL?GCC, stringification, and inline GLSL?(GCC、字符串化和内联 GLSL?)