Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ How fast is passing around objects?

Tags:

c++

Assuming we are running a compiled C++ binary:

Is passing around an int (e.g. function to function, or writing it into variables) slower than passing around structs/class objects like the following?

class myClass
{
      int a;
      int b;
      char c;
      vector d;
      string e;
}
like image 773
wndsr Avatar asked Dec 08 '22 03:12

wndsr


2 Answers

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

like image 192
Matthew Flaschen Avatar answered Dec 09 '22 16:12

Matthew Flaschen


Any time something gets copied how long it takes is going to be a direct result of how big that thing is and what things its copy constructor does; obviously that class is larger than a single int, so it would be slower. If you pass a pointer or pass the thing by reference, there's no copy required and it takes the same amount of time

like image 38
Michael Mrozek Avatar answered Dec 09 '22 17:12

Michael Mrozek