我需要网页处理速度快。要添加的值的计数将是动态的。
以上哪一种更受推荐?请给出合理的原因。
编辑:例如:
string str = "a,b,c"; //Count of the number of elements in str is not fixed
string[] arr = str.Split(',');
或者,
ArrayList al = new ArrayList();
al.Add(str.Split(','));
我需要网页处理速度快。要添加的值的计数将是动态的。
以上哪一种更受推荐?请给出合理的原因。
编辑:例如:
string str = "a,b,c"; //Count of the number of elements in str is not fixed
string[] arr = str.Split(',');
或者,
ArrayList al = new ArrayList();
al.Add(str.Split(','));
List<T>
通常比ArrayList
更优选。
List<T>
更快,因为它避免了装箱。如果您希望向调用者公开的列表是不可变的,则List<T>
和ArrayList
都支持此功能:
List<T>.AsReadOnly()
ArrayList.ReadOnly(ArrayList list);
你的问题涉及到选择使用 ArrayList
还是 List<T>
,但你的示例显示的是一个数组,两者并非同一种数据类型。"不可变"集合使用数组,可变集合使用List<T>
。
性能统计数据(Array vs List vs ReadonlyCollection):
Array List ReadOnlyCollection Penalties Method
00:00:01.3932446 00:00:01.6677450 00:00:06.2444633 1 vs 1,2 vs 4,5 Generate
00:00:00.1856069 00:00:01.0291365 00:00:02.0674881 1 vs 5,5 vs 11,1 Sum
00:00:00.4350745 00:00:00.9422126 00:00:04.5994937 1 vs 2,2 vs 10,6 BlockCopy
00:00:00.2029309 00:00:00.4272936 00:00:02.2941122 1 vs 2,1 vs 11,3 Sort
源代码:
interface IMethods<T>
{
T Generate(int size, Func<int, int> generator);
int Sum(T items);
T BlockCopy(T items);
T Sort(T items);
}
class ArrayMethods:IMethods<int[]>
{
public int[] Generate(int size, Func<int, int> generator)
{
var items = new int[size];
for (var i = 0; i < items.Length; ++i)
items[i] = generator(i);
return items;
}
public int Sum(int[] items)
{
int sum = 0;
foreach (var item in items)
sum += item;
return sum;
}
public int[] BlockCopy(int[] items)
{
var res = new int[items.Length / 2];
Buffer.BlockCopy(items, items.Length / 4 * sizeof(int), res, 0, res.Length * sizeof(int));
return res;
}
public int[] Sort(int[] items)
{
var res = new int[items.Length];
Buffer.BlockCopy(items, 0, res, 0, items.Length * sizeof(int));
return res;
}
}
class ListMethods : IMethods<List<int>>
{
public List<int> Generate(int size, Func<int, int> generator)
{
var items = new List<int>(size);
for (var i = 0; i < size; ++i)
items.Add(generator(i));
return items;
}
public int Sum(List<int> items)
{
int sum = 0;
foreach (var item in items)
sum += item;
return sum;
}
public List<int> BlockCopy(List<int> items)
{
var count = items.Count / 2;
var res = new List<int>(count);
var start = items.Count / 4;
for (var i = 0; i < count; ++i)
res.Add(items[start + i]);
return res;
}
public List<int> Sort(List<int> items)
{
var res = new List<int>(items);
res.Sort();
return res;
}
}
class ReadOnlyCollectionMethods:IMethods<ReadOnlyCollection<int>>
{
public ReadOnlyCollection<int> Generate(int size, Func<int, int> generator)
{
return new ReadOnlyCollection<int>(Enumerable.Range(0, size).Select(generator).ToList());
}
public int Sum(ReadOnlyCollection<int> items)
{
int sum = 0;
foreach (var item in items)
sum += item;
return sum;
}
public ReadOnlyCollection<int> BlockCopy(ReadOnlyCollection<int> items)
{
return new ReadOnlyCollection<int>(items.Skip(items.Count / 4).Take(items.Count / 2).ToArray());
}
public ReadOnlyCollection<int> Sort(ReadOnlyCollection<int> items)
{
return new ReadOnlyCollection<int>(items.OrderBy(s => s).ToList());
}
}
static class Program
{
static Tuple<string, TimeSpan>[] CheckPerformance<T>(IMethods<T> methods) where T:class
{
var stats = new List<Tuple<string, TimeSpan>>();
T source = null;
foreach (var info in new[]
{
new {Name = "Generate", Method = new Func<T, T>(items => methods.Generate(10000000, i => i % 2 == 0 ? -i : i))},
new {Name = "Sum", Method = new Func<T, T>(items => {Console.WriteLine(methods.Sum(items));return items;})},
new {Name = "BlockCopy", Method = new Func<T, T>(items => methods.BlockCopy(items))},
new {Name = "Sort", Method = new Func<T, T>(items => methods.BlockCopy(items))},
new {Name = "Sum", Method = new Func<T, T>(items => {Console.WriteLine(methods.Sum(items));return items;})},
}
)
{
int count = 10;
var stopwatch = new Stopwatch();
stopwatch.Start();
T res = null;
for (var i = 0; i < count; ++i)
res = info.Method(source);
stopwatch.Stop();
source = res;
stats.Add(new Tuple<string, TimeSpan>(info.Name, stopwatch.Elapsed));
}
return stats.ToArray();
}
static void Main()
{
var arrayStats = CheckPerformance(new ArrayMethods());
var listStats = CheckPerformance(new ListMethods());
var rcStats = CheckPerformance(new ReadOnlyCollectionMethods());
Console.WriteLine(" Array List ReadOnlyCollection Penalties Method");
for(var i = 0; i < arrayStats.Length; ++i)
{
Console.WriteLine("{0} {1} {2} 1 vs {3,4:f1} vs {4,4:f1} {5}", arrayStats[i].Item2, listStats[i].Item2, rcStats[i].Item2,
listStats[i].Item2.TotalSeconds / arrayStats[i].Item2.TotalSeconds,
rcStats[i].Item2.TotalSeconds / arrayStats[i].Item2.TotalSeconds, arrayStats[i].Item1);
}
}
IEnumerable<T>
或 IReadOnlyList<T>
(在 .Net 4.5 中新增)是不可变的。 - svickReadOnlyCollection<T>
来包装一个不可变的List<T>
,通常通过调用List<T>.AsReadOnly()
实现。 - JoemyArray[i] = newValue
)。至于性能方面,你的统计数据毫无意义,因为它们没有显示如何进行测量。固定大小(即不能添加/删除元素)与不可变(这意味着现有元素也不能被替换)并不相同。 - JoeList<T>
构造函数中使用容量参数)。 - JoeList <T>
比 ArrayList
总是更快的。 List <T>
不必将添加到其中的值装箱。
ArrayList
只能“接受”对象,这意味着虽然你可以向列表中添加任何对象,但是当你需要这些值时,它们必须由CLR隐式地装箱,然后再由你显式地取消装箱。
List<T>
和ArrayList
使用数组作为其支持存储。它们可以和数组一样快(而且确实如此),但不可能更快。 - Sergey Kalinichenko