C ++ How fast does an object go?

Suppose we run a compiled C ++ binary:

It goes around int (for example, a function for working or writing it to variables) is slower than passing structs / class objects, such as:

 class myClass { int a; int b; char c; vector d; string e; } 
+4
source share
6 answers

This depends on several factors, including the complexity of the copy constructor, and whether the elision compiler can.

+7
source

Anytime when something is copied, how long it takes, it will be a direct result of how big this thing is and what things its copy constructor does; obviously, the class is more than one int, so it will be slower. If you pass a pointer or pass a thing by reference, then no copy is required, and it takes as much time

+3
source

Passing a pointer or a reference to the surrounding object is the same as passing an integer.

However, if you pass actual objects (not pointers to them), you can get copies made from objects, which is expensive. However, many possible copies can be optimized, but still it happens.

+2
source

Passing the myClass instance around is slower than passing the int variable, because the class encapsulates more than int . Rather, you should ask if walking around the various primitives that make up the member variables is slower than passing a single object that wraps them all as one. The answer is no, the two methods should demonstrate the same performance. There is no magic in the class: the class simply associates data with functions if you try to express it yourself without C ++, resulting in the same performance, or, as it may be more, worse than C ++ compilers already give you.

However, C ++ allows you to override the assignment operator for a given type pair, and also allows you to write your own copy constructor so that the instance creates in terms of another instance of the same class. You write these two functions, and therefore the performance of the copy will depend on the performance of these functions. If you do not provide your own copy mechanism, and use one C ++, then the performance is optimal, since the copy is performed in different ways. That is, each member is simply copied.

+2
source

Firstly, a lot depends on whether you pass objects by reference or by value.

In C # and Java, you always pass objects by reference, but in C ++ you can pass a copy of the object on the stack (i.e. pass-by-value). This includes making a physical copy of the object in memory, which implies a couple of calls to the constructor / destructor (at least).

Passing int can be done either by value or by reference. If you pass by value, you copy the int type (let it be 32 bits ) 32 bits stack. If you pass a pointer, you copy the address (again, say 32 bits ) 32 bits stack. In fact, there is no difference in the use of the stack. In the case of passing by reference, the calling function will have to dereference the pointer in order to access the value of the int parameter, so there will be some additional code (and, possibly, performance degradation).

Passing an object (or structure) by value compared to a link is more interesting because they can have very different memory traces (depending on the size of the class / structure).

+1
source

It depends on the members of your class.

Usually you provide a copy constructor that copies all the necessary data to the target instance of your class. The copy constructor is often optimized by the compiler, so there is no difference between passing a class with 2 int fields and just passing 2 integers.

0
source

Source: https://habr.com/ru/post/1306588/


All Articles