c++ - std::is_same 对代码的性能有影响吗?

标签 c++ templates performance-testing

is_same 对代码性能有影响吗?我在我的代码中多次使用它来检查我是否必须使用 std::lessstd::greater 并且根据它们我必须检索特定值。我的测试是否足以证明 std::is_same 不会真正影响我的代码的性能?

代码比那复杂得多,我必须使用模板。我尽我所能模仿使用 is_same 的地方。

我使用 -O3 编译器选项编译并运行了代码。


#include <iostream>
#include <vector>
#include <chrono> 

using namespace std::chrono; 
using namespace std;

template<typename T>
int returnVal(T compare) {
    std::vector<int> v = {1,2,3,4};
    if(std::is_same<T,std::greater<int>>::value) {
        return std::min(v[0], v[1], compare);
    } else {
        return std::min(v[2], v[3], compare);
    }
}

int returnValNoTemplate(bool b) {
    std::vector<int> v = {1,2,3,4};
    if(b == true) {
        return std::min(v[0], v[1]);
    } else {
        return std::min(v[2], v[3]);
    }
}


int main()
{   
    
    for(int i = 0; i < 10; i++) {
      auto start = high_resolution_clock::now(); 
      for(int i  = 0; i < 100000;++i) {
        int x = returnVal(std::greater<int>());
      }
    
      auto stop = high_resolution_clock::now();
      auto duration = duration_cast<microseconds>(stop - start); 
      cout <<"is_same duration:" <<  duration.count() << "\n" << endl; 
    
      auto start1 = high_resolution_clock::now(); 
      for(int i  = 0; i < 100000;++i) {
       int y = returnValNoTemplate(true);
      }
      auto stop1 = high_resolution_clock::now();
      auto duration1 = duration_cast<microseconds>(stop1 - start1); 
      cout <<"No template duration:" <<  duration1.count() << "\n" << endl; 
    }

    return 0;
}

R1: is_same duration:4052 No template duration:4041

R2: is_same duration:3954 No template duration:3950

R3: is_same duration:3963 No template duration:3973

R4: is_same duration:4008 No template duration:4048

R5: is_same duration:3948 No template duration:3998

R6: is_same duration:4130 No template duration:4036

R7: is_same duration:3932 No template duration:3948

R8: is_same duration:4183 No template duration:4088

R9: is_same duration:4731 No template duration:5062

R10: is_same duration:4018 No template duration:4887


#交换测试#

R1:没有模板持续时间:5729 is_same 持续时间:5474

R2:没有模板时长:3988 is_same 时长:4039

R3:没有模板时长:3996 is_same 时长:4114

R4:没有模板时长:4063 is_same 时长:4068

R5:没有模板时长:3979 is_same 时长:4096

R6:没有模板时长:4159 is_same 时长:4020

R7:无模板时长:3990 is_same 时长:4086

R8:没有模板持续时间:4001 is_same 持续时间:4055

R9:没有模板时长:4048 is_same 时长:4088

**R10:**无模板时长:4070 is_same 时长:4017

最佳答案

std:is_same 是一个编译时检查,它是用两个不同的模板足迹实现的。一个有 1 种类型,1 个有 2 种类型。

std::is_same<T, T> //If the compiler resolves to this it is the same type;
std::is_same<T, U> //If the compiler resolves to this it is 2 types;

该实现返回一个类型,然后转换为 bool,该类型解析为 true 或 false,生成代码遵循的路径。

关于c++ - std::is_same 对代码的性能有影响吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63041628/

相关文章:

c++ - 使用 mongocxx 驱动时访问冲突读取位置

c++ - 如何从更高级别在用户级别启动 Exe

linux - 网站加载时间 - Linux (pingdom, google PageSpeed)

c - AES 192、AES-256 的段错误,不适用于 AES-128 位

c++ - 访问 union 中相同类型的非事件成员

c++ - 在 C++ 下调用 asm sqrtsd

C++ 无效数组<T, N> 下标

c++ - C++ 容器的 decltype、通用引用和转发

c++ - 如何使用friend访问STL私有(private)成员

ssl - JMeter:非 HTTP 响应消息:到 URL 的连接被拒绝