LINQ vs vs foreach für die performance-test-Ergebnisse

Kann jemand erklären, diese Ergebnisse?
Ich weiß, es sind doppelte Fragen, aber ich habe noch zu finden, eine einzige Frage, die kam zu dem gleichen Schluss wie meine Ergebnisse 😮

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpeedTest
{
    class Person
    {    
        public Person(string name)
        {
            this.Name = name;
        }

        public string Name { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var people = new List<Person>();
            AddTwins("FRANCISCO", people);
            var stopwatch = new Stopwatch();

            string name = "OCSICNARF";

            long linqTime = 0L;
            long foreachTime = 0L;
            long forTime = 0L;

            stopwatch.Start();
            Person person0;
            var result = from person in people
                         where person.Name == name
                         select person;
            person0 = result.First();
            linqTime = stopwatch.ElapsedMilliseconds;
            stopwatch.Restart();
            Person person1;
            foreach (Person p in people)
            {
                if (p.Name == name)
                {
                    person1 = p;
                    break;
                }
            }
            foreachTime = stopwatch.ElapsedMilliseconds;
            stopwatch.Restart();
            Person person2;
            for (int i = 0; i < people.Count; i++)
            {
                if (people[i].Name == name)
                {
                    person2 = people[i];
                    break;
                }
            }
            forTime = stopwatch.ElapsedMilliseconds;
            stopwatch.Stop();

            Console.WriteLine(string.Format("LINQ took {0}ms", linqTime));
            Console.WriteLine(string.Format("FOREACH took {0}ms", foreachTime));
            Console.WriteLine(string.Format("FOR took {0}ms", forTime));
        }

        static void AddTwins(string name, List<Person> people)
        {
            AddTwins(people, name, "");
        }

        private static void AddTwins(List<Person> people, string choices, string chosen)
        {
            if (choices.Length == 0)
            {
                people.Add(new Person(chosen));
            }
            else
            {
                for (int i = 0; i < choices.Length; i++)
                {
                    //choose
                    char c = choices[i];
                    string choose1 = choices.Substring(0, i);
                    string choose2 = choices.Substring(i + 1);
                    choices = choose1 + choose2;

                    //explore
                    AddTwins(people, choices, chosen + c);

                    //Unchoose
                    string unchoose1 = choices.Substring(0, i);
                    string unchoose2 = choices.Substring(i);
                    choices = unchoose1 + c + unchoose2;
                }
            }
        }
    }
}

LINQ vs vs foreach für die performance-test-Ergebnisse

Schreibe einen Kommentar