简单的函数参数 [英] Simple function arguments

查看:51
本文介绍了简单的函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采用以下简单功能:


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

推荐答案



" JKop" < NU ** @ NULL.NULL>在消息中写道

新闻:Ty ***************** @ news.indigo.ie ...

"JKop" <NU**@NULL.NULL> wrote in message
news:Ty*****************@news.indigo.ie...
拿走以下简单功能:

unsigned long Plus5Percent(无符号长输入)
{
返回(输入+输入/ 20);
}

你有没有考虑过更有效的方法:

unsigned long Plus5Percent(const unsigned long& input)
{
返回(输入+输入/ 20);
}

特别是我指的是转身:

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 of
efficency, with something as small as a long. I simply always do it, out
of habit, whenever I''m not changing the value I''m using. For parameters
which are objects, though, I''d assume that efficiency *is* a reason, since a
const reference doesn''t require a copy of the object to be made. But a copy
of a long or a reference or pointer to a long probably all take the same
space and time. (I can''t say that for sure, but it''d be my guess, at least
on machines where pointers and longs are the same size.)

-Howard


JKop写道:
采取以下简单功能:

unsigned long Plus5Percent(无符号长输入)
{
返回(输入+输入/ 20);
}

你有没有想过可能更有效:

unsigned long Plus5Percent(const unsigned long& input)
{
返回(输入+输入/ 20);
}
...
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 );
}
...




一般情况下,它很大程度上取决于几个与实现相关的

因子。在语言层面上,这个问题没有明确的答案。


-

祝你好运,

Andrey Tarasevich



In general case it heavily depends of several implementation-related
factors. At language level there''s no definitive answer to this question.

--
Best regards,
Andrey Tarasevich




" JKop" < NU ** @ NULL.NULL>在消息中写道

新闻:Ty ***************** @ news.indigo.ie ...

"JKop" <NU**@NULL.NULL> wrote in message
news:Ty*****************@news.indigo.ie...
拿走以下简单功能:

无符号长Plus5Percent(无符号长输入)
返回(输入+输入/ 20);


1. return是关键字,而不是函数,所以

括号是不必要的(但无害)。


2.我可以假设您意识到您的结果将被截断(小数部分被丢弃)吗?


更多信息。

}

你有没有想过可能更有效:


这里的关键词是''可能''。是否

取决于实施和目标

平台。确定的唯一方法是

measure。

unsigned long Plus5Percent(const unsigned long& input)
{
返回(输入+输入/ 20);
}

特别是我指的是转向:

unsigned long Plus5Percent(unsigned long);
进入

unsigned long Plus5Percent(const unsigned long&);
Take the following simple function:

unsigned long Plus5Percent(unsigned long input)
{
return ( input + input / 20 );
1. return is a keyword, not a function, so the
parentheses are unnecessary (but innocuous).

2. May I assume you realize that your result will
be truncated (fractional part is discarded)?

More below.
}

Do yous ever consider the possibly
more efficent:
The key word here is ''possibly''. Whether it is
or not depends upon the implementation and target
platform. The only way to know for sure is to
measure.
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&);




对于内置整数类型,可能没有

的差异(或者通过价值传递可能会更快,因为它通常适合寄存器。)

但对于像这样的东西,如果确实存在性能差异,我怀疑它是否显着。


虽然我们在谈论''表现'',但是是
通常比其他数字操作更昂贵

(除非聪明的优化),所以你可能会考虑:


返回输入* 1.05;


代替。


-Mike



For a built-in integer type, there''s probably no
difference (or perhaps pass by value could be
faster, since it will typically fit in a register.)
But for something like this, if there is indeed
a performance difference, I doubt it''s significant.

While we''re talking of ''performance'', division is
typically more expensive than other numerical operations
(barring clever optimizations), so you might consider:

return input * 1.05;

instead.

-Mike


这篇关于简单的函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆