private static void NonOptimized(AdsContext db)
 {
     var adds = db.Ads.ToList()
         .Where(a => a.AdStatus.Status == "Published")
         .Select(a => new
         {
             a.Title,
             Category = (a.Category == null ? " null" : a.Category.Name),
             Town = (a.Town == null ? " null " : a.Town.Name),
             a.Date
         }).ToList().OrderBy(a => a.Date);
 }
        static void Main(string[] args)
        {

            var db = new AdsContext();
            Console.WriteLine(db.Ads.Count());
            var sw = new Stopwatch();
            sw.Stop();

            SlowVersion(db, sw);


           //OptimizedVersion(db, sw);
        }
Ejemplo n.º 3
0
        private static void WithoutInclude(AdsContext db)
        {
            var adds = db.Ads;

            foreach (var add in adds)
            {
                Console.WriteLine(add.Title + " " + add.AdStatus.Status + " " +
                                  (add.Category == null ? " no category " : add.Category.Name) + " " +
                                  (add.Town == null ? " no town " : add.Town.Name) + " " +
                                  (add.AspNetUser == null ? " no user " : add.AspNetUser.Name)
                                  );
            }
        }
        private static void WithoutInclude(AdsContext db)
        {
            var adds = db.Ads;

            foreach (var add in adds)
            {
                Console.WriteLine(add.Title + " " + add.AdStatus.Status + " " +
                                  (add.Category == null ? " no category " : add.Category.Name) + " " +
                                  (add.Town == null ? " no town " : add.Town.Name) + " " +
                                  (add.AspNetUser == null ? " no user " : add.AspNetUser.Name)
                    );
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            var db = new AdsContext();

            Console.WriteLine(db.Ads.Count());

            var stopWatch = new Stopwatch();

            stopWatch.Start();

            WithoutInclude(db);

            Console.WriteLine(stopWatch.Elapsed);
            stopWatch.Restart();

            WithInclude(db);

            Console.WriteLine(stopWatch.Elapsed);

            stopWatch.Stop();
        }
        static void Main(string[] args)
        {

            var db = new AdsContext();
            Console.WriteLine(db.Ads.Count());

            var stopWatch = new Stopwatch();
            stopWatch.Start();

            WithoutInclude(db);

            Console.WriteLine(stopWatch.Elapsed);
            stopWatch.Restart();

            WithInclude(db);

            Console.WriteLine(stopWatch.Elapsed);

            stopWatch.Stop();

        }
        private static void OptimizedVersion(AdsContext db, Stopwatch sw)
        {
            var addsTitle = db.Ads.Select(a => a.Title);
            sw.Start();
            var fullSelectTime = 0;
            for (int i = 0; i < 10; i++)
            {
                db.Database.ExecuteSqlCommand(@"CHECKPOINT; DBCC DROPCLEANBUFFERS;");
                foreach (var addTitle in addsTitle)
                {
                    Console.WriteLine("         "+addTitle);
                    
                }
                var selectTime = sw.Elapsed.Milliseconds;
                Console.WriteLine(selectTime + " miliseconds");
                fullSelectTime += selectTime;
                sw.Restart();
            }

            Console.WriteLine(fullSelectTime / 10 + " miliseconds - average time ");
            sw.Stop();
        }
        static void Main(string[] args)
        {
            var db= new AdsContext();
            Console.WriteLine(db.Ads.Count());
            Console.WriteLine("---Non Optimized ");
            var sw = new Stopwatch();
            sw.Start();

            var totalNoNOptimizedTime = 0;
            var totalOptimizedTime = 0;

            var nonOpitmizedTotalTime = 0;
            for (int i = 0; i < 10; i++)
            {
                db.Database.ExecuteSqlCommand(@"CHECKPOINT; DBCC DROPCLEANBUFFERS;");
                NonOptimized(db);
                var timeToRun = sw.Elapsed;
                totalNoNOptimizedTime += timeToRun.Milliseconds;
                Console.WriteLine("Time : "+timeToRun.Milliseconds);
                sw.Restart();
            }
            Console.WriteLine("Average time :"+totalNoNOptimizedTime/10);
            Console.WriteLine();
            Console.WriteLine("---Optimized");
            sw.Restart();
            for (int i = 0; i < 10; i++)
            {
                db.Database.ExecuteSqlCommand(@"CHECKPOINT; DBCC DROPCLEANBUFFERS;");
                Optimized(db);
                var timeToRun = sw.Elapsed;
                totalOptimizedTime += timeToRun.Milliseconds;
                Console.WriteLine("Time : "+timeToRun.Milliseconds);
                sw.Restart();
            }
            Console.WriteLine("Average time :"+ totalOptimizedTime/10);
        }