C# 内联函数参数和临时变量

发布于 2024-12-25 02:42:50 字数 936 浏览 3 评论 0原文

我想知道 C# 中的以下情况。

有时函数名称可能非常长且冗长。我在工作中使用 Microsoft 的 MVC 3 框架作为网站,这是一个示例函数:

[ImportModelStateFromTempData]
[BreadCrumb("New Event")]
public ActionResult New()
{
    var @event = _dbContext.EventRepository.CreateNewEvent();

    return View("New", 
        EventViewModel.FromEventDomainModel(@event));
}

可以在不使用临时变量 @event 的情况下重写此代码,如下所示:

[ImportModelStateFromTempData]
[BreadCrumb("New Event")]
public ActionResult New()
{
    return View("New", 
        EventViewModel.FromEventDomainModel(_dbContext.EventRepository.CreateNewEvent()));
}

第一个示例显然更清晰,但从纯粹的好奇心角度来看/性能角度,哪个比另一个更快?特别是考虑到缓存值@event仅使用一次。

在 C++ 中,我记得发现 @event 的局部变量声明(如果这是 C++)将存储在 New() 的堆栈帧中,并且生成的程序集会比直接内联参数稍微慢一些(与将其存储在临时文件中)。

C# 编译器对于这种情况是否更聪明?我可以自由使用临时的而不需要考虑相同的性能吗?

我知道预优化是邪恶的,我绝对不应该担心这种事情,但我对此很好奇。我什至不确定我该去哪里查找有关此问题的更多信息,因为标题是我描述问题的最佳方式。那么你觉得 Stack Overflow 怎么样?

I was wondering about the following situation in C#.

Sometimes function names can be quite long, and verbose. I'm using Microsoft's MVC 3 framework for a website at work, Here's an example function:

[ImportModelStateFromTempData]
[BreadCrumb("New Event")]
public ActionResult New()
{
    var @event = _dbContext.EventRepository.CreateNewEvent();

    return View("New", 
        EventViewModel.FromEventDomainModel(@event));
}

This code could be rewritten without use of the temporary variables @event, like so:

[ImportModelStateFromTempData]
[BreadCrumb("New Event")]
public ActionResult New()
{
    return View("New", 
        EventViewModel.FromEventDomainModel(_dbContext.EventRepository.CreateNewEvent()));
}

The first example is obviously more clear, but from a pure curiosity perspective/performance perspective, is either faster than the other? Especially considering that the cached value @event is only being used once.

In C++ I remember finding out that the local variable declaration of @event (if this were C++) would be stored in New()'s stack frame, and the assembly generated would be SLIGHTLY slower than directly in-lining the argument (as opposed to storing it in a temporary).

Is the C# compiler smarter about this situation? Am I free to use temporary's without the same performance considerations?

I understand that pre-optimization is evil, and I absolutely should not be worried about this kind of this, but I am curious about this. I'm not even sure about where I would go looking up more information about this, as the title was the best way I could describe my question. So what do you think Stack Overflow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

携君以终年 2025-01-01 02:42:50

这些产生的 IL 将是等效的。如果您想向自己证明这一点,请尝试编译两者并使用 Ildasm http://msdn.microsoft.com/en-us/library/f7dy01k1(v=vs.80).aspx

The IL produced by these will be equivalent. If you want to prove it to yourself, try compiling both and looking at the resulting IL with Ildasm http://msdn.microsoft.com/en-us/library/f7dy01k1(v=vs.80).aspx

只是我以为 2025-01-01 02:42:50

临时变量是好的。

调试时,可以检查函数的结果。

延迟(如果有)以纳秒或皮秒为单位。

保养为王。

Temporary variables are good.

When you are debugging, you can check the results of functions.

The delay, if there is one, is in nano or pico seconds.

Maintenance is king.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文