Quick Q: What makes moving objects faster than copying?

Quick A: it allows to steal ressources instead of creating new ones.

Recently on SO:

What makes moving objects faster than copying?

It's all about implementation. Consider simple string class:

class my_string {
  char* ptr;
  size_t capacity;
  size_t length;
};

Semantics of copy requires us to make a full copy of string including allocation of another array in dynamic memory and copying *ptr contents there, which is expensive.

Semantics of move requires us only to transfer the value of pointer itself to new object without duplicating contents of string.

If, of course, class doesn't use dynamic memory or system resources, then there is no difference between moving and copying in terms of performance.

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.