本文介绍了简单的函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 采用以下简单功能: unsigned long Plus5Percent(无符号长输入) { return(输入+输入/ 20); } 你有没有考虑过更有效的方法: unsigned long Plus5Percent(const unsigned long& amp ;输入) { 返回(输入+输入/ 20); } 特别是我'' m指转向: unsigned long Plus5Percent(unsigned long); 进入 unsigned long Plus5Percent( const unsigned long&); -JKop Take the following simple function: unsigned long Plus5Percent(unsigned long input){return ( input + input / 20 );}Do yous ever consider the possibly more efficent: unsigned long Plus5Percent(const unsigned long& input){return ( input + input / 20 );}Specifically I''m referring to turning:unsigned long Plus5Percent(unsigned long); into unsigned long Plus5Percent(const unsigned long&);-JKop推荐答案 " Yous"?哟,你不会碰巧来自费城,不是吗? :-) 我通常不会考虑使用const引用,为了 效率,只需要很长的东西。我只是总是这样做,只要我没有改变我正在使用的价值,就会产生习惯。但是对于作为对象的参数 ,我会假设效率*是一个原因,因为 const引用不需要副本要制造的对象。但是一个副本 一个长或一个引用或指向一个long可能都会占用相同的空间和时间。 (我不能肯定地说,但这是我的猜测,至少 指针和长指数相同的机器。) -Howard "Yous"? Yo, yous wouldn''t happen to be from Philly, would yous? :-) I don''t usually consider using a const reference, for the purposes ofefficency, with something as small as a long. I simply always do it, outof habit, whenever I''m not changing the value I''m using. For parameterswhich are objects, though, I''d assume that efficiency *is* a reason, since aconst reference doesn''t require a copy of the object to be made. But a copyof a long or a reference or pointer to a long probably all take the samespace and time. (I can''t say that for sure, but it''d be my guess, at leaston machines where pointers and longs are the same size.) -Howard 一般情况下,它很大程度上取决于几个与实现相关的 因子。在语言层面上,这个问题没有明确的答案。 - 祝你好运, Andrey Tarasevich In general case it heavily depends of several implementation-relatedfactors. At language level there''s no definitive answer to this question. --Best regards,Andrey Tarasevich 对于内置整数类型,可能没有 的差异(或者通过价值传递可能会更快,因为它通常适合寄存器。) 但对于像这样的东西,如果确实存在性能差异,我怀疑它是否显着。 虽然我们在谈论''表现'',但是是通常比其他数字操作更昂贵 (除非聪明的优化),所以你可能会考虑: 返回输入* 1.05; 代替。 -Mike For a built-in integer type, there''s probably nodifference (or perhaps pass by value could befaster, since it will typically fit in a register.)But for something like this, if there is indeeda performance difference, I doubt it''s significant. While we''re talking of ''performance'', division istypically more expensive than other numerical operations(barring clever optimizations), so you might consider: return input * 1.05; instead. -Mike 这篇关于简单的函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-02 16:17