哪种数据类型运算效率更高?意外的测试结果
一般总是以为整数的运算效率更高,然而今天的测试结果却颇感意外。Double 居然是最佳的选择 ……
采用 Debug 配置编译运行输出结果:
Decimal 4796
Double 375
Single 344
Int64 531
Int32 360
除 Decimal 比较慢以外,其他几个都差不多,
采用 Release 配置编译,运行结果:
Decimal 4688
Double 140
Single 157
Int64 421
Int32 32
Int32 效率显著提高,但Double 居然比 Int64要快!也比 Single 快,这真是意外 !也就是说,综合考虑效率和值范围等因素,Double 应该是最佳的选择。
[TestMethod]
public void Testcase()
{
int starttime, endtime;
int length = 10000000;
decimal ddd1 = 1, ddd = 1234, ddd2 = 2;
starttime = System.Environment.TickCount;
for (int i = 0; i < length; i++)
{
ddd *= ddd2;
ddd -= ddd1;
ddd /= ddd2;
ddd += ddd1;
}
endtime = System.Environment.TickCount;
System.Console.Out.WriteLine("{1} {0}", endtime - starttime, ddd.GetType().Name);
double fff1 = 1, fff = 1234, fff2 = 2;
starttime = System.Environment.TickCount;
for (int i = 0; i < length; i++)
{
fff *= fff2;
fff -= fff1;
fff /= fff2;
fff += fff1;
}
endtime = System.Environment.TickCount;
System.Console.Out.WriteLine("{1} {0}", endtime - starttime, fff.GetType().Name);
float sss1 = 1, sss = 1234, sss2 = 2;
starttime = System.Environment.TickCount;
for (int i = 0; i < length; i++)
{
sss *= sss2;
sss -= sss1;
sss /= sss2;
sss += sss1;
}
endtime = System.Environment.TickCount;
System.Console.Out.WriteLine("{1} {0}", endtime - starttime, sss.GetType().Name);
long lll1 = 1, lll = 1234, lll2 = 2;
starttime = System.Environment.TickCount;
for (int i = 0; i < length; i++)
{
lll *= lll2;
lll -= lll1;
lll /= lll2;
lll += lll1;
}
endtime = System.Environment.TickCount;
System.Console.Out.WriteLine("{1} {0}", endtime - starttime, lll.GetType().Name);
int iii1 = 1, iii = 1234, iii2 = 2;
starttime = System.Environment.TickCount;
for (int i = 0; i < length; i++)
{
iii *= iii2;
iii -= iii1;
iii /= iii2;
iii += iii1;
}
endtime = System.Environment.TickCount;
System.Console.Out.WriteLine("{1} {0}", endtime - starttime, iii.GetType().Name);
}
public void Testcase()
{
int starttime, endtime;
int length = 10000000;
decimal ddd1 = 1, ddd = 1234, ddd2 = 2;
starttime = System.Environment.TickCount;
for (int i = 0; i < length; i++)
{
ddd *= ddd2;
ddd -= ddd1;
ddd /= ddd2;
ddd += ddd1;
}
endtime = System.Environment.TickCount;
System.Console.Out.WriteLine("{1} {0}", endtime - starttime, ddd.GetType().Name);
double fff1 = 1, fff = 1234, fff2 = 2;
starttime = System.Environment.TickCount;
for (int i = 0; i < length; i++)
{
fff *= fff2;
fff -= fff1;
fff /= fff2;
fff += fff1;
}
endtime = System.Environment.TickCount;
System.Console.Out.WriteLine("{1} {0}", endtime - starttime, fff.GetType().Name);
float sss1 = 1, sss = 1234, sss2 = 2;
starttime = System.Environment.TickCount;
for (int i = 0; i < length; i++)
{
sss *= sss2;
sss -= sss1;
sss /= sss2;
sss += sss1;
}
endtime = System.Environment.TickCount;
System.Console.Out.WriteLine("{1} {0}", endtime - starttime, sss.GetType().Name);
long lll1 = 1, lll = 1234, lll2 = 2;
starttime = System.Environment.TickCount;
for (int i = 0; i < length; i++)
{
lll *= lll2;
lll -= lll1;
lll /= lll2;
lll += lll1;
}
endtime = System.Environment.TickCount;
System.Console.Out.WriteLine("{1} {0}", endtime - starttime, lll.GetType().Name);
int iii1 = 1, iii = 1234, iii2 = 2;
starttime = System.Environment.TickCount;
for (int i = 0; i < length; i++)
{
iii *= iii2;
iii -= iii1;
iii /= iii2;
iii += iii1;
}
endtime = System.Environment.TickCount;
System.Console.Out.WriteLine("{1} {0}", endtime - starttime, iii.GetType().Name);
}
采用 Debug 配置编译运行输出结果:
Decimal 4796
Double 375
Single 344
Int64 531
Int32 360
除 Decimal 比较慢以外,其他几个都差不多,
采用 Release 配置编译,运行结果:
Decimal 4688
Double 140
Single 157
Int64 421
Int32 32
Int32 效率显著提高,但Double 居然比 Int64要快!也比 Single 快,这真是意外 !也就是说,综合考虑效率和值范围等因素,Double 应该是最佳的选择。