SFINAE的性能,如果不是
The performance of SFINAE and if else
我知道if else
可能会生成管道停滞(气泡(,因为 Branch 预测器无法保持 100% 的正确猜测。总之,很多if elif elif ... else
的表现都很糟糕。
在C++的模板中,我们有SFINAE。使用 SFINAE,我们可以避免if else
代码。例如,要检查 int 是否为奇数,我们可以按如下方式编码:
template <int I> void div(char(*)[I % 2 == 0] = 0) {
// this overload is selected when I is even
}
template <int I> void div(char(*)[I % 2 == 1] = 0) {
// this overload is selected when I is odd
}
这样我们就可以避免
if (I % 2 == 0)
{
// do things
}
else
{
// do other things
}
我的问题是,SFINAE与if else
相比有更好的性能吗?SFINAE可以避免管道泡沫吗?
从运行时性能的角度来看,你做哪一个根本不重要。 任何一个I
在编译时都是可知的,在这种情况下,任何半体面的编译器都会为两者输出相同的常量情况,或者不是,在这种情况下,SFINAE 方式根本不会编译。
相关文章:
- 如果不在窗口 10 中声明名为 openCV 的 openCV namedWindow,QT 图像显示将无法正常工作
- 如果不是多个语句,请使用 if 语句
- 如果不包含 pthread,为什么 GCC 的线程标准库实现会抛出异常?
- C++我们可以取消引用此指针吗?如果是这样,那么如何,如果不是,那为什么?
- 如果不分配内存,我如何能够为变量创建和分配值?
- 如果不初始化结构中的向量,它会自动为空还是具有随机内存位置的值?
- 删除 QGraphicsPixmapItem (如果它已添加到场景中).如果不添加到场景
- 从库中发出信息,而无需运行时成本(如果不需要)
- 可能的模板和constexpr - 如果不兼容
- 如果不是这样,我需要帮助理解这个嵌套的问题
- 如果不需要易失性,为什么 std::atomic 方法会提供易失性重载
- 代码是否有效.如果我想显示第一个元素?如果不是,那么 s.begin() 会返回什么?
- 如果不手动完成,子类是否继承父类的析构函数?
- 如果不是十六进制,则QT从qlineedit中删除字符
- 如果不有效,如何重复用户输入电子邮件
- RE2 语法:如果不共享相同的前缀,则跳过匹配
- 如果不允许使用constexpr,为什么sfinae在上面
- fscanf:如果不返回 EOF,是否可能出现错误或 EOF?
- 打开文件,如果不存在,则创建,确定是否创建
- SFINAE的性能,如果不是