将const应用于原始类型时,按引用进行调用会提高性能吗? [英] Does const call by reference improve performance when applied to primitive types?
问题描述
对于对象(尤其是字符串),按引用调用比按值调用要快,因为函数调用不需要创建原始对象的副本.使用const,还可以确保引用不被滥用.
我的问题是,如果使用基本类型(例如bool,int或double),按引用进行const调用是否也更快?
void doSomething(const string & strInput, unsigned int iMode);
void doSomething(const string & strInput, const unsigned int & iMode);
我的怀疑是,一旦原始类型的字节大小超过地址值的大小,使用按引用调用便是有利的.即使差异很小,我也想利用这一优势,因为我经常调用其中一些函数.
其他问题:内联对我的问题的答案有影响吗?
我的怀疑是,一旦原始类型的字节大小超过地址值的大小,使用按引用调用便是有利的.即使差异很小,我也想利用这一优势,因为我经常调用其中一些函数.
基于预感的性能调整在C ++中大约占0%的时间(这是我对统计数据的直觉,它通常可以正常工作...)
如果sizeof(T) > sizeof(ptr)
,const T&
小于T
是正确的,因此通常为32位或64位,具体取决于系统.
现在问自己:
1)多少个内置类型大于64位?
2)复制不值得使代码不清楚的32位吗?如果您的功能由于没有复制32位值而变得明显更快,那么它可能没有太大作用?
3)你真的那么聪明吗? (扰流板警报:否.)请参阅此好答案,因为它几乎总是一个坏主意: https://stackoverflow.com/a/4705871/1098041
最终仅按值传递.如果经过(彻底)分析后发现某个功能是瓶颈,而您尝试的所有其他优化还不够(并且您应该在此之前尝试大多数优化),请通过const引用传递.>
然后看到它没有任何改变.翻滚和哭泣.
Concerning objects (especially strings), call by reference is faster than call-by-value because the function call does not need to create a copy of the original object. Using const, one can also ensure that the reference is not abused.
My question is whether const call-by-reference is also faster if 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 advantageous to use call-by-reference as soon as the primitive type's size in bytes exceeds the size of the address value. Even if the difference is small, I'd like to take the advantage because I call some of these functions quite often.
Additional question: Does inlining have an influence on the answer to my question?
My suspicion is that it is advantageous to use call-by-reference as soon as the primitive type's size in bytes exceeds the size of the address value. Even if the difference is small, I'd like to take the advantage because I call some of these functions quite often.
Performance tweaking based on hunches works about 0% of the time in C++ (that's is a gut feeling I have about statistics, it works usually...)
It is correct that the const T&
will be smaller than the T
if sizeof(T) > sizeof(ptr)
, so usually 32-bits, or 64, depending on the system..
Now ask yourself :
1) How many built-in types are bigger than 64 bits ?
2) Is not copying 32-bits worth making the code less clear ? If your function becomes significantly faster because you didn't copy a 32bit value to it, maybe it doesn't do much ?
3) Are you really that clever ? (spoiler alert : no.) See this great answer for the reason why it is almost always a bad idea : https://stackoverflow.com/a/4705871/1098041
Ultimately just pass by value. If after (thorough) profiling you identify that some function is a bottleneck, and all of the other optimizations that you tried weren't enough (and you should try most of them before this), pass-by-const-reference.
Then See that it doesn't change anything. roll-over and cry.
这篇关于将const应用于原始类型时,按引用进行调用会提高性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!