示例#1
0
        static void AnyAndAllExampleDynamic()
        {
            var       allAndAny = new AllAndAny(false);
            Stopwatch timer     = new Stopwatch();

            timer.Start();
            if (allAndAny.RealtimeDownloadedEnumerable.Any(x => x > 10000))
            {
                EnumerateAndPrintValues(allAndAny.RealtimeDownloadedEnumerable.Where(x => x > 10000));
            }
            timer.Stop();
            var millisecondsForEnumeration = timer.ElapsedMilliseconds;

            Console.WriteLine($"\nDynamic Collection:\nTotal Time Taken: {millisecondsForEnumeration}\nInitialization Time: {0}\nEnumeration Time: {millisecondsForEnumeration}");

            //Results are Not Idempotent
            timer.Reset();
            timer.Start();
            var firstPass  = allAndAny.RealtimeDownloadedEnumerable.Where(x => x > 10000);
            var secondPass = allAndAny.RealtimeDownloadedEnumerable.Where(x => x > 10000);
            //You can not ignore the warning related to multiple enumerations below because multiple enumerations will yield different results and each enumeration would be costly
            var equalNumberElements      = firstPass.Count() == secondPass.Count();
            var eachElementExistsInOther = firstPass.All(thisItem => secondPass.Any(thatItem => thisItem == thatItem));

            timer.Stop();
            Console.WriteLine($"\n\n**Dynamic Collection**\nBoth Passes have Equal Items: {equalNumberElements}\nBoth passes have exact same elements: {eachElementExistsInOther}");
            Console.WriteLine($"Total Time Taken in All and Any Operations: {timer.ElapsedMilliseconds}");
        }
示例#2
0
        static void AnyAndAllExampleConcrete()
        {
            /*
             * There are a few things to care about when using any and all on IEnumerable.
             *  1. On Concrete Implementations and Pure Functions, you get predictable and deterministic results
             *  2. On dynamic data sources returning IEnumerable because there is mutation involved results are not predictable and non deterministic.
             */
            Stopwatch timer = new Stopwatch();

            timer.Start();
            var allAndAny = new AllAndAny();

            timer.Stop();
            var millisecondsForInit = timer.ElapsedMilliseconds;

            timer.Reset();
            timer.Start();
            if (allAndAny.ListBackedEnumerable.Any(x => x > 10000))
            {
                EnumerateAndPrintValues(allAndAny.ListBackedEnumerable.Where(x => x > 10000));
            }
            timer.Stop();
            var millisecondsForEnumeration = timer.ElapsedMilliseconds;

            Console.WriteLine($"\nConcrete Collection:\nTotal Time Taken: {millisecondsForInit+ millisecondsForEnumeration}\nInitialization Time: {millisecondsForInit}\nEnumeration Time: {millisecondsForEnumeration}");

            //Results are Idempotent (unless side effected)
            timer.Reset();
            timer.Start();
            var firstPass  = allAndAny.ListBackedEnumerable.Where(x => x > 10000);
            var secondPass = allAndAny.ListBackedEnumerable.Where(x => x > 10000);
            //You can ignore the warning related to multiple enumerations below
            var equalNumberElements      = firstPass.Count() == secondPass.Count();
            var eachElementExistsInOther = firstPass.All(thisItem => secondPass.Any(thatItem => thisItem == thatItem));

            timer.Stop();
            Console.WriteLine($"\n\n**Concrete Collection**\nBoth Passes have Equal Items: {equalNumberElements}\nBoth passes have exact same elements: {eachElementExistsInOther}");
            Console.WriteLine($"Total Time Taken in All and Any Operations: {timer.ElapsedMilliseconds}");
        }