相关文章推荐

List.FindAll的效率竟然比for循环还差
这里写图片描述

以上是baidu出来的一篇文章,让我惊诧不已,因为在AI模块我已经使用了很多FindAll。但是,上文的结论是真的吗?我在上文代码基础上增加了多次测试的代码:

        static void Main(string[] args)
            Test(1);
            Test(1);
            Test(10);
            Test(100);
            Test(1000);
        static void Test(int count)
            Console.WriteLine("【 count = {0} 】", count);
            Random rand = new Random();
            List<int> list = new List<int>();
            for (int i = 0; i < 100; i++)
                list.Add(rand.Next(10));
            Stopwatch watch = new Stopwatch();
            //用findall
            long time = 0;
            for (int j = 0; j < count; j++)
                list.Clear();
                for (int i = 0; i < 100; i++)
                    list.Add(rand.Next(10));
                watch.Reset();
                watch.Start();
                List<int> resultfindall = null;
                resultfindall = list.FindAll((i) => { return i == 5; });
                watch.Stop();
                time += watch.Elapsed.Ticks;
            Console.WriteLine("findall:" + time);
            //用for循环
            List<int> resultfor = null;
            time = 0;
            for (int j = 0; j < count; j++)
                list.Clear();
                for (int i = 0; i < 100; i++)
                    list.Add(rand.Next(10));
                watch.Reset();
                watch.Start();
                resultfor = new List<int>(100);
                for (int i = 0; i < list.Count; i++)
                    if (list[i] == 5)
                        resultfor.Add(list[i]);
                watch.Stop();
                time += watch.Elapsed.Ticks;
            Console.WriteLine("for:" + time);

得到了如下结果:

.Net2.0, visual studio 执行1,1,10, 100,1000次:
这里写图片描述
.Net4.1, visual studio 执行1,1,10, 100,1000次:
这里写图片描述
Unity 先预处理再执行1000次:
这里写图片描述
Unity 先预处理再执行 10 次
这里写图片描述
Unity 先预处理再执行 1 次
这里写图片描述

我的测试程序表明:无论windows平台还是unity平台,List.FindAll的效率并非如前文所说“比for循环差五六十倍”。事实是,因为要初始化算法现场,进程第一次调用List.FindAll的耗时比for循环长五六十倍。但是,第一次之后的调用List.FindAll和for循环的性能差异是非常小的,可以忽略。

List《T》.FindAll的效率竟然比for循环还差 以上是baidu出来的一篇文章,让我惊诧不已,因为在AI模块我已经使用了很多FindAll。但是,上文的结论是真的吗?我在上文代码基础上增加了多次测试的代码: static void Main(string[] args) { Test(1); Test(1); 其中泛型T为list定义的时候用户决定的存储类型,Predicate match 是一个委托,可理解为函数指针,实质上被定义为 public delegate bool Predicate (T obj) 则开发者可以使用自己... 1.在foreach和list.ForEach中使用list.Remove() 在foreach中是不能使用list.Remove(),否则在进入下一个循环就会报异常,所以,如果有使用之后就必须break; 在.ForEach()中,要注意,在.net framework 4.5 会报异常:集合已修改;可能无法执行枚举操作。在.net framework4 3.5 3.0 2.0这几个版本下,可以直接使用list.Remove(),但如果在使用Remove()后下一
在开发过程中,有时会需要在List中查找某个特定元素,这时我们可以使用List的Find方法来实现,实例如下: 1.首先我们定义一个Student类来模拟我们所需要的的类,每个student有他的学号(Number)和姓氏(LastName)。 public class student public int Number; public string LastName; 2.建立一个脚本,声明Students为List,并声明几个student在List中。 private List<s
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; List<int> evenNumbers = numbers.Where(n => n % 2 == 0).ToList(); foreach (int number in evenNumbers) Console.WriteLine(number); 在上面的示例中,我们使用Where方法和lambda表达式来筛选出列表中的偶数。然后,我们使用ToList方法将结果转换为一个新的列表。最后,我们遍历新列表并打印出每个偶数。 请注意,使用LINQ可能需要引入System.Linq命名空间,并且你可以根据自己的需求更改lambda表达式来进行不同的筛选条件。
 
推荐文章