C++ and QT4.5 - is passing a const int& overkill? Does pass by reference helps in signals/slots?

前端 未结 3 894
-上瘾入骨i
-上瘾入骨i 2021-02-15 16:02

Two questions rolled into one here...

I have a number of functions which are called multiple times per frame for a real-time video processing application. Taking advice

相关标签:
3条回答
  • 2021-02-15 16:17

    Yes, this is overkill and will actually result in slower code than if you passed the int by value. An int is four bytes; a reference (essentially a memory address) is either also four bytes (on a 32-bit machine) or eight bytes (on a 64-bit machine). So you may actually need to pass more information to the function -- and additionally, you have the overhead of dereferencing that reference.

    If you're passing something bigger than an int, however, it's more efficient to use a const reference, because you can pass just four or eight bytes instead of having to copy the whole object.

    Edit Regarding Qt: Yes, if the slot takes a const reference to an object, then the reason for that is to save the overhead of copying the object.

    0 讨论(0)
  • 2021-02-15 16:23

    First - the difference between

    • void processSomething(const int& value);
    • void processSomething(int value);

    is: normally a const reference is passed by passing a pointer, the other thing is passed by copying. After the call (from the caller side) the result is equal. Whatever you pass to the function is not changed by the call.

    Inside the function you won't see any differences either (at least on an int). On Objects, you may only use const functions of course.

    On the performance side - passing a const reference to an int may (or may not) be slower, depending on compiler and optimization. The compiler could (in theory) optimize the passing of the const reference away to a pass by value, I don't know if it does though. Working with pointers to ints instead of values is slower of course.

    For more information on that see: When to use pointers, and when not to use them


    After reading the other linked post - the compiler can not optimize it away in a multithreaded environment. At least in a MT-environment there are real differences betwenn const int& and int calls. +1 for that link.

    0 讨论(0)
  • 2021-02-15 16:24

    Yes, pass by reference helps against copying the objects. But the compiler might decide to optimize it out entirely and insted pass by value if it produces the same effect. Such optimization is usually possible if the function and the call site are in the same translation unit but some compilers can do even more global optimizations - you can inspect the emitted assembly if you care.

    This is why you really shouldn't pass primitive types by reference if you care about performance and unless you really have reasons for that. See What is the use of passing const references to primitive types? for discussion of that problem.

    0 讨论(0)
提交回复
热议问题