gpt4 book ai didi

c# - lambda 表达式或 get_property 函数在 C# 中的实际作用是什么?

转载 作者:行者123 更新时间:2023-12-05 03:16:22 24 4
gpt4 key购买 nike

public class People
{
private string _name;
public string Name { get; set; }

public string SName;

public string LamName => _name;
public People(string s)
{
_name = s;
Name = s;
}

public string GetName1()
{
return _name;
}

public string GetName2()
{
return Name;
}

public void OnlyFunc()
{

}
}

class Program
{
int interCount = 100000000;

public void AsAttribute()
{
People people = new People("Wave");
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < interCount; ++i)
{
object val = people.SName;
}
stopwatch.Stop();
Console.WriteLine("AsAttribute: {0}ms", stopwatch.ElapsedMilliseconds);
}

public void Directly()
{
People people = new People("Wave");
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < interCount; i++)
{
object value = people.Name;
}
stopwatch.Stop();

Console.WriteLine("Directly: {0}ms", stopwatch.ElapsedMilliseconds);
}

public void UseFunc1()
{
People people = new People("Wave");
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < interCount; i++)
{
object value = people.GetName1();
}
stopwatch.Stop();

Console.WriteLine("UseFunc1: {0}ms", stopwatch.ElapsedMilliseconds);
}

public void UseFunc2()
{
People people = new People("Wave");
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < interCount; i++)
{
object value = people.GetName2();
}
stopwatch.Stop();

Console.WriteLine("UseFunc2: {0}ms", stopwatch.ElapsedMilliseconds);
}

public void Lambda()
{
People people = new People("Wave");
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < interCount; i++)
{
object value = people.LamName;
}
stopwatch.Stop();

Console.WriteLine("Lambda: {0}ms", stopwatch.ElapsedMilliseconds);
}

static void Main(string[] args)
{
Program p = new Program();
p.AsAttribute();
p.Directly();
p.Lambda();
p.UseFunc1();
p.UseFunc2();
}
}

这是我测试的例子,我得到的时间成本结果如下:

Screenshot

AsAttribute: 82ms
Directly: 213ms
Lambda: 229ms
UseFunc1: 255ms
UseFunc2: 418ms

我想知道是什么导致了差异,当我将其标记为属性时,它获得最佳性能,当我使用 lambda 表达式或函数获取属性时,性能下降。

最佳答案

从代码中可以看出第一次运行是现场运行,而不是属性。(所以是最快的,看IL上的代码就知道了)而direct就是对属性的直接执行。Lambda 表达式等同于 get {return ...} ,因此与 Directly 没有区别。UseFunc1 通过调用该方法很慢。UseFunc2 是最慢的,因为它执行两个步骤,如 mehtod --> property。

这是关于您的 C# 代码的 il 代码

私有(private)字符串_name;

[CompilerGenerated]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string <Name>k__BackingField;

public string SName;

public string Name
{
[CompilerGenerated]
get
{
return <Name>k__BackingField;
}
[CompilerGenerated]
set
{
<Name>k__BackingField = value;
}
}

public string LamName
{
get
{
return _name;
}
}

public People(string s)
{
_name = s;
Name = s;
}

public string GetName1()
{
return _name;
}

public string GetName2()
{
return Name;
}

关于c# - lambda 表达式或 get_property 函数在 C# 中的实际作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74697923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com