TransWikia.com

does std::is_same has an impact on the performance of the code?

Stack Overflow Asked on November 29, 2021

Does is_same has any impact on the code performance? I am using it multiple times in my code to check whether i have to use std::less or std::greater and depending on them i have to retrieve specific values. Is my test sufficient to prove that std::is_same does not really impact the performance of the code in my case?

The code is much more complex than that and I have to use templates. I tried as much as i can to mimic the places where is_same is used.

I compiled and ran the code with -O3 compiler option.


#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


#Swaping tests#

R1: No template duration:5729 is_same duration:5474

R2: No template duration:3988 is_same duration:4039

R3: No template duration:3996 is_same duration:4114

R4: No template duration:4063 is_same duration:4068

R5: No template duration:3979 is_same duration:4096

R6: No template duration:4159 is_same duration:4020

R7: No template duration:3990 is_same duration:4086

R8: No template duration:4001 is_same duration:4055

R9: No template duration:4048 is_same duration:4088

**R10:**No template duration:4070 is_same duration:4017

One Answer

std:is_same is a compile time check it is implemented with two different template footprints. One with 1 Type and 1 with 2 Types.

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;

The implementation returns a type that then has a conversion to bool which resolves to true or false generating one pathway for the code to follow.

Answered by yugami on November 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP