public static string str = "test";
static void Main(string[] args)
{
test.test1("test1");
test.test1("test");
test.test2("test1");
test.test2("test");
test.test3("test1");
test.test3("test");
test.test4("test1");
test.test4("test");
Console.ReadLine();
}
public static class test
{
public static void test1(string str1)
{
int time = 100000000;//计算一亿次
int start, end;
bool result=false;
start = System.Environment.TickCount;
for (int i = 0; i < time; i++)
{
result =str.Equals(str1);
}
if (result==true)
{
Console.WriteLine("结果为真时采用str1.Equals(value)方式花了:");
}
else
{
Console.WriteLine("结果为假时采用str1.Equals(value)方式花了:");
}
end = System.Environment.TickCount;
Console.WriteLine((end - start));
}
public static void test2(string str1)
{
int time = 100000000;//计算一亿次
int start, end;
bool result = false;
start = System.Environment.TickCount;
for (int i = 0; i < time; i++)
{
result = (str==str1);
}
if (result == true)
{
Console.WriteLine("结果为真时采用str==value方式花了:");
}
else
{
Console.WriteLine("结果为假时采用str==value方式花了:");
}
end = System.Environment.TickCount;
Console.WriteLine((end - start));
}
public static void test3(string str1)
{
int time = 100000000;//计算一亿次
int start, end;
bool result = false;
start = System.Environment.TickCount;
for (int i = 0; i < time; i++)
{
result = (str1 == str);
}
if (result == true)
{
Console.WriteLine("结果为真时采用value==str方式花了:");
}
else
{
Console.WriteLine("结果为假时采用value==str方式花了:");
}
end = System.Environment.TickCount;
Console.WriteLine((end - start));
}
public static void test4(string str1)
{
int time = 100000000;//计算一亿次
int start, end;
bool result = false;
start = System.Environment.TickCount;
for (int i = 0; i < time; i++)
{
result = str1.Equals(str);
}
if (result == true)
{
Console.WriteLine("结果为真时采用value.Equals(str1)方式花了:");
}
else
{
Console.WriteLine("结果为假时采用value.Equals(str1)方式花了:");
}
end = System.Environment.TickCount;
Console.WriteLine((end - start));
}
static void Main(string[] args)
{
test.test1("test1");
test.test1("test");
test.test2("test1");
test.test2("test");
test.test3("test1");
test.test3("test");
test.test4("test1");
test.test4("test");
Console.ReadLine();
}
public static class test
{
public static void test1(string str1)
{
int time = 100000000;//计算一亿次
int start, end;
bool result=false;
start = System.Environment.TickCount;
for (int i = 0; i < time; i++)
{
result =str.Equals(str1);
}
if (result==true)
{
Console.WriteLine("结果为真时采用str1.Equals(value)方式花了:");
}
else
{
Console.WriteLine("结果为假时采用str1.Equals(value)方式花了:");
}
end = System.Environment.TickCount;
Console.WriteLine((end - start));
}
public static void test2(string str1)
{
int time = 100000000;//计算一亿次
int start, end;
bool result = false;
start = System.Environment.TickCount;
for (int i = 0; i < time; i++)
{
result = (str==str1);
}
if (result == true)
{
Console.WriteLine("结果为真时采用str==value方式花了:");
}
else
{
Console.WriteLine("结果为假时采用str==value方式花了:");
}
end = System.Environment.TickCount;
Console.WriteLine((end - start));
}
public static void test3(string str1)
{
int time = 100000000;//计算一亿次
int start, end;
bool result = false;
start = System.Environment.TickCount;
for (int i = 0; i < time; i++)
{
result = (str1 == str);
}
if (result == true)
{
Console.WriteLine("结果为真时采用value==str方式花了:");
}
else
{
Console.WriteLine("结果为假时采用value==str方式花了:");
}
end = System.Environment.TickCount;
Console.WriteLine((end - start));
}
public static void test4(string str1)
{
int time = 100000000;//计算一亿次
int start, end;
bool result = false;
start = System.Environment.TickCount;
for (int i = 0; i < time; i++)
{
result = str1.Equals(str);
}
if (result == true)
{
Console.WriteLine("结果为真时采用value.Equals(str1)方式花了:");
}
else
{
Console.WriteLine("结果为假时采用value.Equals(str1)方式花了:");
}
end = System.Environment.TickCount;
Console.WriteLine((end - start));
}
}
测试结论:当判断大部分情况下为真时选择==,当判断大部分为假时选择equals,value写在前还是后,个人觉得写在前面不容易把==误写成=,因为变量是不可以赋值给字符串的,反过来就可以。