110,537
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
var x = new List<object>();
for (int j = 0; j < 10; j++)
{
x.Add(j);
}
}
sw.Stop();
Console.WriteLine(sw.ElapsedTicks);
sw.Reset();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
var y = new ArrayList();
for (int j = 0; j < 10; j++)
{
y.Add(j);
}
}
sw.Stop();
Console.WriteLine(sw.ElapsedTicks);
}
}
}
17402617
17170626
Press any key to continue . . .using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
var x = new List<object>();
for (int j = 0; j < 100; j++)
{
x.Add(j);
}
}
sw.Stop();
Console.WriteLine(sw.ElapsedTicks);
sw.Reset();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
var y = new ArrayList();
for (int j = 0; j < 100; j++)
{
y.Add(j);
}
}
sw.Stop();
Console.WriteLine(sw.ElapsedTicks);
}
}
}
测试100个元素的插入,算小数据量了吧。同样在一个数量级上。
94096078
107058149
Press any key to continue . . .using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
List<object>[] a1 = new List<object>[10000000];
for (int i = 0; i < 10000000; i++)
{
a1[i] = new List<object>();
}
sw.Stop();
Console.WriteLine(sw.ElapsedTicks);
sw.Reset();
sw.Start();
ArrayList[] a2 = new ArrayList[10000000];
for (int i = 0; i < 10000000; i++)
{
a2[i] = new ArrayList();
}
sw.Stop();
Console.WriteLine(sw.ElapsedTicks);
}
}
}
90071119
91062186
Press any key to continue . . .
运行了10多次,互有胜负。