Example #1
0
        static void Main()
        {
            var db = new AdsEntities();
            var ads = db.Ads.ToList();

            // Variant 1.
            var starTime = DateTime.Now;
            var ads1 = db.Ads
                .First();

            var endTime = DateTime.Now;

            Console.WriteLine(endTime - starTime);

            // Variant 2.
            starTime = DateTime.Now;

            var ads2 = db.Ads
                .Select(a => a.Title)
                .First();

            endTime = DateTime.Now;

            Console.WriteLine(endTime - starTime);
        }
        public static void SelectEverything(AdsEntities context)
        {
            var testQuery = context.Ads.Select(a => a);

            foreach (var ad in testQuery)
            {
                Console.WriteLine(ad.Title);
            }
        }
        public static void SelectCertainColumn(AdsEntities context)
        {
            var testQuery = context.Ads.Select(a => a.Title);

            foreach (var ad in testQuery)
            {
                Console.WriteLine(ad);
            }
        }
 private static void Optimized(AdsEntities context)
 {
     var allAds = context.Ads
         .Where(a => a.AdStatus.Status == "Published")
         .OrderBy(a => a.Date)
         .Select(a => new
         {
             a.Title,
             Category = a.Category != null ? a.Category.Name : null,
             Town = a.Town != null ? a.Town.Name : null,
             a.Date
         });
 }
        static void Main()
        {
            var context = new AdsEntities();
            //This neutralize the first time connect to base that takes time
            context.Ads.Count();
            context.Database.ExecuteSqlCommand("CHECKPOINT");
            context.Database.ExecuteSqlCommand("DBCC DROPCLEANBUFFERS");

            double sum = 0;
            int runs = 1;

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

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Run #{0}", runs++);
                SelectEverything(context);
                double time = stopwatch.Elapsed.TotalSeconds;
                Console.WriteLine("----{0:F4}", time);
                sum += time;
            }

            Console.WriteLine("Average time non-optimized: {0:F4}", sum / 10);
            Console.WriteLine();

            runs = 1;
            double sum2 = 0;

            stopwatch.Restart();

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Run #{0}", runs++);
                SelectCertainColumn(context);
                double time = stopwatch.Elapsed.TotalSeconds;
                Console.WriteLine("----{0:F4}", time);
                sum2 += time;
            }

            Console.WriteLine("Average time optimized: {0:F4}", sum2 / 10);
        }
        static void Main()
        {
            var context = new AdsEntities();

            //Count of queries = 28
            foreach (var ad in context.Ads)
            {
                Console.WriteLine("{0} - {1} \n Category: {2} \n Town: {3} \n User: {4}\n",
                    ad.Title, ad.AdStatus, ad.Category, ad.Town, ad.AspNetUser);
            }

            //Count of queries = 1
            foreach (var ad in context.Ads
                .Include(a => a.AdStatus)
                .Include(a => a.Category)
                .Include(a => a.Town)
                .Include(a => a.AspNetUser))
            {
                Console.WriteLine("Ad Status: {0}\nCategory: {1}\nTown: {2}\nUser: {3}",
                    ad.AdStatus, ad.Category, ad.Town, ad.AspNetUser);
            }
        }
        static void Main()
        {
            var context = new AdsEntities();
            //This neutralize the first time connect to base that takes time
            context.Ads.Count();

            double sum = 0;
            int runs = 1;

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

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Run #{0}", runs++);
                NonOptimized(context);
                double time = stopwatch.Elapsed.TotalSeconds;
                Console.WriteLine("{0:F4}", time);
                sum += time;
            }
            Console.WriteLine("Average time non-optimized: {0:F4}", sum / 10);
            Console.WriteLine();

            runs = 1;
            double sum2 = 0;

            stopwatch.Restart();

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Run #{0}", runs++);
                Optimized(context);
                double time = stopwatch.Elapsed.TotalSeconds;
                Console.WriteLine("{0:F4}", time);
                sum2 += time;
            }

            Console.WriteLine("Average time optimized: {0:F4}", sum2 / 10);
        }
Example #8
0
        static void Main()
        {
            var db = new AdsEntities();

            //// Variant 1. CountOfQueries = 1
            //var ads = db.Ads.Select(a => new
            //{
            //    a.Title,
            //    a.AdStatus,
            //    a.Category,
            //    a.Town,
            //    a.AspNetUser
            //});

            //foreach (var ad in ads)
            //{
            //    Console.WriteLine("{0} - {1} \n Category: {2}; Town: {3} \n User: {4}\n",
            //        ad.Title, ad.AdStatus, ad.Category, ad.Town, ad.AspNetUser);
            //}

            // Variant 2. CountOfQueries = 30
            foreach (var ad in db.Ads)
            {
                Console.WriteLine("{0} - {1} \n Category: {2}; Town: {3} \n User: {4}\n",
                    ad.Title, ad.AdStatus, ad.Category, ad.Town, ad.AspNetUser);
            }

            //// Variant 3. CountOfQueries = 1
            //foreach (var ad in db.Ads
            //    .Include(a => a.AdStatus)
            //    .Include(a => a.Town)
            //    .Include(a => a.Category)
            //    .Include(a => a.AspNetUser))
            //{
            //    Console.WriteLine("{0} - {1} \n Category: {2}; Town: {3} \n User: {4}\n",
            //        ad.Title, ad.AdStatus, ad.Category, ad.Town, ad.AspNetUser);
            //}
        }