您的位置:首页 > 其它

成员变量,属性,自动属性 的性能差异.

2011-10-31 00:14 197 查看
首先,建立一个类.

public class KVTable
{
public string Name;
}


书写代码

var kvt = new KVTable ();

My.Test.BeginTestTick();

for (int i = 0; i < 1000000; i++)
{
kvt.Name = i.ToString();
}

My.Test.EndTestTickAndShow();


执行用时 600 毫秒

改为

public class KVTable
{
public string Name{get;set;}
}


执行用时 2511 毫秒

改为

public class KVTable
{
private string name;

public string Name
{
get { return name; }
set { name = value; }
}
}


执行用时 2511 毫秒.

结论. 属性 和 自动属性性能相同. 成员变量性能要远高于前两者.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: