[英]Fastest way to add a Range in a List C#
I have to add about 3000 Elements to more than 20 Lists.我必须将大约3000 个元素添加到20多个列表中。 The code that I am using:
我正在使用的代码:
LastChange = new List<string>();
LastChange.AddRange(Enumerable.Repeat("/", 3000));
same for more than 20 Lists...
Is there any "fastest way" to add "/" to the Lists or that is the best solution.是否有任何“最快的方法”将“/”添加到列表中,或者这是最好的解决方案。 Thanks in Advance...
提前致谢...
A bit better solution is to allocate memory for 3000
items in the constructor in order to avoid memory reallocation:更好的解决方案是在构造函数中为
3000
项目分配内存以避免内存重新分配:
LastChange = new List<string>(3000);
LastChange.AddRange(Enumerable.Repeat("/", 3000));
You can go further and get rid of IEnumerator<T>
in Enumerable.Repeat
:你可以更进一步,去掉
Enumerable.Repeat
的IEnumerator<T>
:
int count = 3000;
LastChange = new List<string>(count);
// compare to 0 is a bit faster then 3000
for (int i = count - 1; i >= 0; --i)
LastChange[i] = "\";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.