内联C#函数 [英] Inlining of C# functions

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

问题描述

您好,


有人能告诉我C#编译器是否会自动内联简单的
函数?具体来说,给定:


double RadiansToDegrees(双学位){

返回学位* 180.0 / Math.PI;

}


将类似

y = RadiansToDegrees(x);

编译为

y = x * 180.0 / Math.PI;

而不是函数调用?


另外,我是否正确假设将评估180.0 / Math.PI在编译

时间而不是运行时间?


谢谢。

解决方案



" Sheila Jones" < SH ********** @ btopenworld.com>在消息中写道

新闻:Ou ************* @ TK2MSFTNGP11.phx.gbl ...

你好,

有人能告诉我C#编译器是否会自动内联简单的功能?具体来说,给出:

双RadiansToDegrees(双度){
返回度* 180.0 / Math.PI;
}

会像
y = RadiansToDegrees(x);
编译为
y = x * 180.0 / Math.PI;
而不是函数调用?
它将被编译为函数调用,但JIT可以自由内联它生成的

代码。它可能会考虑简单的方法。
另外,我是否正确假设180.0 / Math.PI将在
编译时而不是运行时进行评估?
我不知道,虽然我怀疑如此。我建议通过ILDasm进行一次

旅行,并附带一些代码来查看。
谢谢。





是的,JIT编译器将内联你的方法,因为它看到

适合内联它,除非你的类或其基础之一

类,来自System.MarshalByReference

类。 System.MarshalByReference类抑制后代的所有

内联...


- 理查德

- ---原始消息-----
您好,

任何人都可以告诉我C#编译器是否会自动
内联简单函数?具体来说,给出:

双RadiansToDegrees(双度){
返回度* 180.0 / Math.PI;
}

会像
y = RadiansToDegrees(x);
编译为
y = x * 180.0 / Math.PI;
而不是函数调用?

另外,我是否正确假设180.0 / Math.PI将在编译时而不是运行时评估


谢谢。





" Richard" <一个******* @ discussions.microsoft.com>在留言中写道

新闻:e4 **************************** @ phx.gbl ... < blockquote class =post_quotes>
是的,JIT编译器将内联你的方法,因为它看起来适合内联它,除非你的类或它的一个基类类来自系统.MarshalByReference
类。 System.MarshalByReference类抑制后代的所有内联......

那实际上是System.MarshalByRefObject ,;)。 - 理查德

-----原始消息-----
您好,

任何人都可以告诉我C#编译器是否会自动


内联简单

函数?具体来说,给出:

双RadiansToDegrees(双度){
返回度* 180.0 / Math.PI;
}

会像
y = RadiansToDegrees(x);
编译为
y = x * 180.0 / Math.PI;
而不是函数调用?

另外,我是否正确假设180.0 / Math.PI将在

时间而不是运行时间进行


评估?

谢谢。



Hello,

Can anybody tell me if the C# compiler will automatically inline simple
functions? To be specific, given:

double RadiansToDegrees(double degrees) {
return degrees*180.0/Math.PI;
}

will something like
y = RadiansToDegrees(x);
be compiled as
y = x*180.0/Math.PI;
rather than a function call?

Also, am I correct to assume that 180.0/Math.PI will be evaluated at compile
time rather than run time?

Thanks.

解决方案


"Sheila Jones" <sh**********@btopenworld.com> wrote in message
news:Ou*************@TK2MSFTNGP11.phx.gbl...

Hello,

Can anybody tell me if the C# compiler will automatically inline simple
functions? To be specific, given:

double RadiansToDegrees(double degrees) {
return degrees*180.0/Math.PI;
}

will something like
y = RadiansToDegrees(x);
be compiled as
y = x*180.0/Math.PI;
rather than a function call? It will be compiled as a function call, but the JIT is free to inline the
code it generates. It probably will considering the simplicitly of method.
Also, am I correct to assume that 180.0/Math.PI will be evaluated at compile time rather than run time? I don''t know, off hand, although I would suspect so. I''d suggest taking a
trip through ILDasm with a bit of code that does it and see.
Thanks.




Yes, the JIT compiler will inline your method as it sees
fit to inline it UNLESS your class, or one of its base
classes, descends from the System.MarshalByReference
class. The System.MarshalByReference class suppresses all
inlining for descendants...

--Richard

-----Original Message-----
Hello,

Can anybody tell me if the C# compiler will automatically inline simplefunctions? To be specific, given:

double RadiansToDegrees(double degrees) {
return degrees*180.0/Math.PI;
}

will something like
y = RadiansToDegrees(x);
be compiled as
y = x*180.0/Math.PI;
rather than a function call?

Also, am I correct to assume that 180.0/Math.PI will be evaluated at compiletime rather than run time?

Thanks.
.




"Richard" <an*******@discussions.microsoft.com> wrote in message
news:e4****************************@phx.gbl...


Yes, the JIT compiler will inline your method as it sees
fit to inline it UNLESS your class, or one of its base
classes, descends from the System.MarshalByReference
class. The System.MarshalByReference class suppresses all
inlining for descendants...
That would actually be System.MarshalByRefObject, ;). --Richard

-----Original Message-----
Hello,

Can anybody tell me if the C# compiler will automatically


inline simple

functions? To be specific, given:

double RadiansToDegrees(double degrees) {
return degrees*180.0/Math.PI;
}

will something like
y = RadiansToDegrees(x);
be compiled as
y = x*180.0/Math.PI;
rather than a function call?

Also, am I correct to assume that 180.0/Math.PI will be


evaluated at compile

time rather than run time?

Thanks.
.



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

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