What is faster: calling the noop function or if statement?

I have a function pointer for the deleter, but most of the time, the deletor will not be needed only when I save an internal copy of something. I am currently doing this with the noop deleter function:

class MyClass{
public:
  // bind object
  template<class Type>
  void Bind(Type* obj){
    Cleanup();
    object_ = obj;
  }

  // bind object with internal copy
  template<class Type>
  void Bind(Type obj){
    Cleanup();
    object_ = new Type(obj);
    deleter = &Deleter<Type>;
  }

private:
  template<class Type>
  static void Deleter(void* obj_ptr){
    Type* obj = static_cast<Type*>(obj_ptr);
    delete obj;
  }

  static void NoopDeleter(void* unused){
  }

  void Cleanup(){
    (*deleter_)(object_);
    object_ = 0;
    deleter_ = &NoopDeleter;
  }

  typedef void (*DeleterFunc)(void*);

  void* object_;
  DeleterFunc deleter_;
};

Now, the obvious other choice would be to install it 0when it is not needed, and test the function Cleanupwith if(deleter_ != 0) (*deleter_)(object_).
Now, while coding, it occurred to me: β€œHmm, which version will be faster?”, So this is more of a personal interest than an optimization. Sorry if the question sounds kind of silly, but it bothers me and I really want to know. Thanks for any answers in advance!

+3
source share
2 answers

. if . , , rchitecture .

+3

0 ( , ), , . , object_ , ....

+1

All Articles