C++ QT4.5 - 传递一个 const int&矫枉过正?通过引用传递对信号/槽有帮助吗?
这里有两个问题合二为一......
我有许多函数,对于实时视频处理应用程序,每帧调用多次。考虑到关于 const 和通过引用传递的建议,这些函数的签名有点像这样
void processSomething(const int& value);
当我不断输入一些额外的字符时,我想知道这是否有点过头了。
第二个问题,关于按引用传递的主题,在 QT 的槽/信号机制中,按引用传递是否有助于防止像正常函数调用中那样复制对象?
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 about const and pass by reference, the functions have a signature somewhat like this
void processSomething(const int& value);
As I keep typing the few extra characters, I am wondering if this is overkill.
Second question, well, on the subject of pass by reference, within the slots/signals mechanism of QT, does pass by reference helps to prevent copying of objects as in a normal function call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这有点过分了,实际上会导致代码比按值传递 int 慢。一个 int 是四个字节;引用(本质上是内存地址)也是四个字节(在 32 位机器上)或八个字节(在 64 位机器上)。因此,您实际上可能需要向函数传递更多信息——此外,您还有取消引用该引用的开销。
但是,如果您传递的值大于 int,则使用 const 引用会更有效,因为您可以仅传递四个或八个字节,而不必复制整个对象。
编辑关于Qt:是的,如果槽采用对对象的const引用,那么这样做的原因是为了节省复制对象的开销。
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.
是的,通过引用传递有助于防止复制对象。但是,如果产生相同的效果,编译器可能会决定完全优化它并插入按值传递。如果函数和调用站点位于同一翻译单元中,这种优化通常是可能的,但某些编译器甚至可以进行更多的全局优化 - 如果您关心的话,您可以检查发出的程序集。
这就是为什么如果您关心性能并且除非您确实有理由这样做,那么您确实不应该通过引用传递基元类型。请参阅 将 const 引用传递给原始类型? 来讨论这个问题。
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.
之间的区别
是:通常 const 引用是通过传递指针来传递的,其他东西是通过复制来传递的。调用后(从调用方)结果是相等的。传递给函数的任何内容都不会因调用而改变。
在函数内部,您也不会看到任何差异(至少在 int 上)。当然,在对象上,您只能使用 const 函数。
在性能方面 - 将 const 引用传递给 int 可能(也可能不会)更慢,具体取决于编译器和优化。编译器可以(理论上)优化将 const 引用传递给按值传递,但我不知道是否可以。当然,使用指向整数的指针而不是值会更慢。
有关详细信息,请参阅:
何时使用指针,何时不使用使用它们
阅读其他链接的帖子后 - 编译器无法在多线程环境中对其进行优化。至少在 MT 环境中,const int 和 const int 之间存在真正的区别。和 int 调用。该链接+1。
First - the difference between
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.