Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ CRTP比动态调度花费更多的时间_C++_Inline_Crtp - Fatal编程技术网

C++ CRTP比动态调度花费更多的时间

C++ CRTP比动态调度花费更多的时间,c++,inline,crtp,C++,Inline,Crtp,下面我分享了CRTP实现和动态调度的代码。在使用分析编译器代码时,我可以检查CRTP案例中没有函数调用CRTPInterface::tick的内嵌。由于函数不在行中,因此会消耗更多的时间 如何强制编译器使其内联?我尝试过使用opton-finline函数,但没有成功 CRTP实施: #include <iostream> #include <chrono> using namespace std; using namespace std::chrono; templat

下面我分享了CRTP实现和动态调度的代码。在使用分析编译器代码时,我可以检查CRTP案例中没有函数调用CRTPInterface::tick的内嵌。由于函数不在行中,因此会消耗更多的时间

如何强制编译器使其内联?我尝试过使用opton-finline函数,但没有成功

CRTP实施:

#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;

template <typename Implementation>
class CRTPInterface {
public:
  void tick(uint64_t n) {
    impl().tick(n);
  }

  uint64_t getvalue() {
    return impl().getvalue();
  }
private:
  Implementation& impl() {
    return *static_cast<Implementation*>(this);
  }
};

class CRTPImplementation : public CRTPInterface<CRTPImplementation> {
  uint64_t counter;
public:
  CRTPImplementation()
    : counter(0) {
  }

  inline void tick(uint64_t n) {
    counter += n;
  }

  uint64_t getvalue() {
    return counter;
  }
};
const unsigned N = 6000;
template <typename Implementation>
void run_crtp(CRTPInterface<Implementation> obj) {
  for (unsigned i = 0; i < N; ++i) {
    for (unsigned j = 0; j < i; ++j) {
      obj.tick(j);
    }
  }
}

int main()
{


    CRTPImplementation crtoObj;
    auto start= high_resolution_clock::now();
    run_crtp<CRTPImplementation>(crtoObj);


    auto stop= high_resolution_clock::now();
    auto duration=duration_cast<milliseconds>(stop-start);
    cout<<"Time taken= "<<duration.count()<<endl;



    return 0;
}
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;

class DynamicInterface {
public:
  virtual void tick(uint64_t n) = 0;
  virtual uint64_t getvalue() = 0;
};

class DynamicImplementation : public DynamicInterface {
  uint64_t counter;

public:
  DynamicImplementation()
    : counter(0) {
  }

  virtual void tick(uint64_t n) {
    counter += n;
  }

  virtual uint64_t getvalue() {
    return counter;
  }
};

const unsigned N = 6000;

void run_dynamic(DynamicInterface* obj) {
  for (unsigned i = 0; i < N; ++i) {
    for (unsigned j = 0; j < i; ++j) {
      obj->tick(j);
    }
  }
}
int main()
{
    DynamicImplementation diObj;

    auto start= high_resolution_clock::now();
    run_dynamic(&diObj);
    auto stop= high_resolution_clock::now();
    auto duration=duration_cast<milliseconds>(stop-start);
    cout<<"Time taken= "<<duration.count()<<endl;

    return 0;
}
#包括
#包括
使用名称空间std;
使用名称空间std::chrono;
模板
类crtp接口{
公众:
无效勾号(uint64\n){
impl()。勾选(n);
}
uint64_t getvalue(){
返回impl().getvalue();
}
私人:
实现&impl(){
返回*static_cast(此);
}
};
类CRTPImplementation:公共CRTPInterface{
uint64_t计数器;
公众:
crtp实现()
:计数器(0){
}
内联无效勾号(uint64\u t n){
计数器+=n;
}
uint64_t getvalue(){
返回计数器;
}
};
常数无符号N=6000;
模板
无效运行\u crtp(crtp接口obj){
for(无符号i=0;icoutSure看起来像是内联到地狱,回到我的身边:你在使用什么编译器/标志?编辑:更好的测试,使用强制的副作用来防止它崩溃为无操作。如果你不启用优化,编译器将不会内联任何内容。我想知道*static_cast(这个)"正在混淆编译器,以某种方式阻止内联。@SvenNilsson,这正是他使用CRTP的原因,以避免虚拟调度。感谢@AlanBirtles…随着-O2优化CRTP性能的显著提高,-O2优化级别可以安全使用吗?看起来它就像是内联到地狱一样,又回到了我:什么你正在使用的编译器/标志?编辑:更好的测试,使用强制的副作用来防止它崩溃为不操作。如果你不启用优化,编译器不会内联任何内容。我想知道*static_cast(这个)“混淆了编译器,以某种方式阻止了内联。@SvenNilsson使用CRTP以避免虚拟调度的原因正是因为它
static\u cast
。感谢@AlanBirtles…,因为-O2优化CRTP性能显著提高。使用-O2优化级别安全吗?”?