namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
checkarray();//checkclass(); }
static void checkclass()
{
uu[] a = new uu[10000];
int start, stop;
start = Environment.TickCount;
for (int m = 0; m < 100; m++)
{
for (int n = 0; n < 10000; n++)
{
a[n] = new uu();
a[n].a++;
}
}
stop = Environment.TickCount;
Console.Write(stop - start);
}
static void checkarray()
{
int[] a = new int[10000];
int start, stop;
start = Environment.TickCount;
for (int m = 0; m < 100; m++)
{
for (int n = 0; n < 10000; n++)
{ a[n]++;
}
}
stop = Environment.TickCount;
Console.Write(stop - start);
} }
class uu
{
public int a = 0;
}
checkclass()运行时间:32ms
checkarray()运行时间:3ms
同样的简单程序用C++,时间都差不多,均为3ms
我的理解是C#中对类的处理耗用了大量的时间,大家来讨论一下
{
class Program
{
static void Main(string[] args)
{
checkarray();//checkclass(); }
static void checkclass()
{
uu[] a = new uu[10000];
int start, stop;
start = Environment.TickCount;
for (int m = 0; m < 100; m++)
{
for (int n = 0; n < 10000; n++)
{
a[n] = new uu();
a[n].a++;
}
}
stop = Environment.TickCount;
Console.Write(stop - start);
}
static void checkarray()
{
int[] a = new int[10000];
int start, stop;
start = Environment.TickCount;
for (int m = 0; m < 100; m++)
{
for (int n = 0; n < 10000; n++)
{ a[n]++;
}
}
stop = Environment.TickCount;
Console.Write(stop - start);
} }
class uu
{
public int a = 0;
}
checkclass()运行时间:32ms
checkarray()运行时间:3ms
同样的简单程序用C++,时间都差不多,均为3ms
我的理解是C#中对类的处理耗用了大量的时间,大家来讨论一下
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货