C# .Net代码格式和#x27;内衬';

C# .Net代码格式和#x27;内衬';,c#,C#,考虑这个人为的例子: public static string ContrivedOne(Guid id, string host) { Contact person = new Contact(host, id); DbRow record = new DbRow(host); string msg = string.Format("Hi, {0}", host); StuffItems stuff = GetStuff(id); string resu

考虑这个人为的例子:

public static string ContrivedOne(Guid id, string host)
{
    Contact person = new Contact(host, id);
    DbRow record = new DbRow(host);
    string msg = string.Format("Hi, {0}", host);
    StuffItems stuff = GetStuff(id);
    string result = record.Save(person, stuff, msg);
    return result;
}


public static string ContrivedOner(Guid id, string host)
{
    return new DbRow(host).Save(new Contact(host, id), GetStuff(id), string.Format("Hi, {0}", host));
}

在所有条件相同的情况下,不考虑易读性等因素,它们之间的性能通常会有差异吗?

我能看到的性能上的主要差异是,当它们被放在堆栈上时,会有什么

在第一个示例中,调用每个函数,并将其返回值放在堆栈上的局部变量中。在第二种情况下,“Save”函数调用可能会被推送,然后是参数中的每个调用(仍然存储到一个隐藏的局部变量)


当然,所有这些细节都是由编译器决定的。至于实际的(读取的,明显的)性能差异,您不会看到任何东西。

很可能不会。如果不在实践中进行实际测试,两者都不能说始终更快,虽然从技术上讲,由于缓存和方法的调用顺序不同,可能会有微小的差异。这证实了我的怀疑:有时编译器会想出一些“聪明”的东西/更快,有时不会。所以,就像约阿希姆建议的那样,你必须测试每个案例。但总的来说,潜在的收益并不能抵消痛苦。谢谢