1. C#
DateTime dtStart = DateTime.Now;
for (double i = 0; i < 100000000; i++)
{
int temp = 0;
if (temp==0)
{
temp = 1;
}
else
{
temp = 2;
}
}
DateTime dtEnd = DateTime.Now;
Console.WriteLine(dtEnd.Millisecond – dtStart.Millisecond);
for (double i = 0; i < 100000000; i++)
{
int temp = 0;
if (temp==0)
{
temp = 1;
}
else
{
temp = 2;
}
}
DateTime dtEnd = DateTime.Now;
Console.WriteLine(dtEnd.Millisecond – dtStart.Millisecond);
dtStart = DateTime.Now;
for (double i = 0; i < 100000000; i++)
{
int temp = 0;
temp = temp == 0 ? 1 : 2;
}
dtEnd = DateTime.Now;
Console.WriteLine(dtEnd.Millisecond – dtStart.Millisecond);
for (double i = 0; i < 100000000; i++)
{
int temp = 0;
temp = temp == 0 ? 1 : 2;
}
dtEnd = DateTime.Now;
Console.WriteLine(dtEnd.Millisecond – dtStart.Millisecond);
Console.ReadLine();
输出:0小时0分0秒628毫秒
0小时0分0秒480毫秒
以上C#代码测试得出结论:同样的操作三元运算符的效率高。
转载于:https://www.cnblogs.com/love-wen-qin-free/archive/2012/01/31/2332734.html