标签:
有这样的一个类,类里有相同的一段代码,一个是方法是一个是属性,谁的执行效率会更高:
public class Test
{
public static bool A = false;
public static string Name
{
get
{
if (A)
return Convert.ToString(1 + 1);
else
return Convert.ToString(2 + 1);
}
}
public static string MatctName()
{
if (A)
return Convert.ToString(1 + 1);
else
return Convert.ToString(2 + 1);
}
}
用下面的方法执行:
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i <= 1000000; i++)
{
string name = Test.Name;
}
watch.Stop();
Console.WriteLine("Name:" + watch.ElapsedTicks.ToString());
watch.Start();
for (int i = 0; i <= 1000000; i++)
{
string name = Test.MatctName();
}
watch.Stop();
Console.WriteLine("MatctName():" + watch.ElapsedTicks.ToString());
Console.ReadKey();
执行结果:
标签:
原文地址:http://www.cnblogs.com/sangame1214/p/4703203.html