Does matching by reference improve performance when applied to primitive types?

As for objects (especially strings), calling by reference is faster than calling by value, because calling a function does not require creating a copy of the original object. Using const can also ensure that a link is not abused.

My question is whether const call-by-reference is faster when using primitive types like bool, int or double.

void doSomething(const string & strInput, unsigned int iMode); void doSomething(const string & strInput, const unsigned int & iMode); 

My suspicion is that it is useful to use call-by-reference as soon as the size of the primitive type in bytes exceeds the size of the address value. Even if the difference is small, I would like to take advantage of this because I often call some of these functions.

Additional question: does inlining affect the answer to my question?

+5
source share
5 answers

My suspicion is that it is useful to use call-by-reference as soon as the size of the primitive type in bytes exceeds the size of the address value. Even if the difference is small, I would like to take advantage of this because I often call some of these functions.

Hunches-based performance training runs about 0% of the time in C ++ (it feels like I have statistics, it usually works ...)

It is const T& that const T& will be less than T if sizeof(T) > sizeof(ptr) , therefore usually 32 bits or 64, depending on the system.

Now ask yourself:

1) How many built-in types are more than 64 bits?

2) Does 32-bit code copy to make the code less understandable? If your function becomes much faster because you did not copy the 32-bit value to it, maybe it is not so much?

3) Are you really that smart? (spoiler warning: no). Look at this great answer for the reason that it is almost always a bad idea: fooobar.com/questions/111666 / ...

Ultimately, just go by value. If after (thorough) profiling you find that some function is a bottleneck, and all the other optimizations that you tried are not enough (and you should try most of them before), pass-by-const-reference.

Then let's see that it does not change anything. roll around and cry.

+2
source

In addition to the other answers, I would like to note that when you pass a link and use (aka dereferencing), which is a lot in your function, it can be slower than making a copy.

This is because the local variables for the function (usually) are loaded into the cache together, but when one of them is a pointer / reference, and the function uses this, it can lead to a cache miss. This means that you need to go to the (slower) main memory to get a pointer to a variable, which can be slower than copying, which is loaded into the cache along with the function.

Thus, even for "small objects" it is potentially faster to simply pass by value.

(I read this in a very good book: Computer Systems: The Perspective of Programmers )

Another interesting discussion of the whole hit / skip topic: How do I write the code that best uses the processor cache to improve performance?

+3
source

const is a keyword that evaluates to compiletime. This does not affect runtime performance. You can read more about it here: https://isocpp.org/wiki/faq/const-correctness

+2
source

There is no primitive type in 64-bit architecture - at least not in C ++ 11 --- which is more than a pointer / link. You should check this, but intuitively, there should be the same amount of data moving around for const T& as for int64_t , and less for any primitive where sizeof(T) < sizeof(int64_t) . Therefore, since you can measure any difference, passing primitives by value should be faster if your compiler does the obvious thing - that’s why I emphasize that if you need certainty here, you should write a test case.

Another consideration is that the parameters of primitive functions can end in processor registers, which makes access to them as fast as memory access. You may find that more instructions are created for your const T& parameters than for your T parameters when T is primitive. You can verify this by checking the assembler output by your compiler.

+2
source

I was taught:

  • Passing by value when an argument variable is one of the main built-in types, for example bool , int or float . Objects of these types are so small that passing by reference does not increase efficiency. Also, if you want to make a copy of the variable.

  • Pass a constant if you want to efficiently pass a value that you don't need to change.

  • Pass the link only if you want to change the value of the argument variable. But try to avoid changing variable arguments whenever possible.

0
source

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


All Articles