当前位置: 首页>>代码示例>>C#>>正文


C# TimeSpan.Average方法代码示例

本文整理汇总了C#中System.TimeSpan.Average方法的典型用法代码示例。如果您正苦于以下问题:C# TimeSpan.Average方法的具体用法?C# TimeSpan.Average怎么用?C# TimeSpan.Average使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.TimeSpan的用法示例。


在下文中一共展示了TimeSpan.Average方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        public static void Main()
        {
            var context = new AdsEntities();

            TimeSpan[] nonOptimizedTestDurations = new TimeSpan[10];
            TimeSpan[] optimizedTestDurations = new TimeSpan[10];

            Console.WriteLine("Non-optimized:");
            for (int i = 0; i < 10; i++)
            {
                nonOptimizedTestDurations[i] = GetTimeForNonOptimizedSelect(context);
                Console.WriteLine("Run {0}: {1}", i + 1, nonOptimizedTestDurations[i]);
            }

            Console.WriteLine("\nOptimized:");
            for (int i = 0; i < 10; i++)
            {
                optimizedTestDurations[i] = GetTimeForOptimizedSelect(context);
                Console.WriteLine("Run {0}: {1}", i + 1, optimizedTestDurations[i]);
            }

            Console.WriteLine("\nNon-optimized avarage: {0} Ticks{1}Optimized avarage: {2} Ticks",
                nonOptimizedTestDurations.Average(n => n.Ticks),
                    Environment.NewLine,
                    optimizedTestDurations.Average(n => n.Ticks));
        }
开发者ID:Martin-Andreev,项目名称:Database-Applications,代码行数:26,代码来源:SelectMain.cs

示例2: Main

        public static void Main()
        {
            var context = new AdsEntities();
            //// Initialize connection
            Console.WriteLine("All ads = " + context.Ads.Count());
            string clearCache = "CHECKPOINT; DBCC DROPCLEANBUFFERS;";
            TimeSpan[] nonOptimizedArray = new TimeSpan[6];
            TimeSpan[] optimizedArray = new TimeSpan[6];

            for (int i = 0; i < 6; i++)
            {
                Stopwatch sw = new Stopwatch();
                context.Database.ExecuteSqlCommand(clearCache);

                sw.Start();
                foreach (var ad in context.Ads)
                {
                    Console.WriteLine(ad.Title);
                }

                nonOptimizedArray[i] = sw.Elapsed;

                context.Database.ExecuteSqlCommand(clearCache);

                sw.Restart();
                foreach (var adTitle in context.Ads.Select(a => a.Title))
                {
                    Console.WriteLine(adTitle);
                }

                optimizedArray[i] = sw.Elapsed;
                sw.Stop();
            }

            string table = string.Format(
            @"
                       Run 1              Run 2              Run 3              Run 4              Run 5              Run 6         Average time (ms)
             --------------- ------------------ ------------------ ------------------ ------------------ ------------------ ------------------ -------------------
              Non-optimized   {0}   {1}   {2}   {3}   {4}   {5}   {6:F3}
              Optimized       {7}   {8}   {9}   {10}   {11}   {12}   {13:F3}",
            nonOptimizedArray[0],
            nonOptimizedArray[1],
            nonOptimizedArray[2],
            nonOptimizedArray[3],
            nonOptimizedArray[4],
            nonOptimizedArray[5],
            nonOptimizedArray.Average(n => n.Milliseconds),
            optimizedArray[0],
            optimizedArray[1],
            optimizedArray[2],
            optimizedArray[3],
            optimizedArray[4],
            optimizedArray[5],
            optimizedArray.Average(o => o.Milliseconds));
            File.WriteAllText(@"..\..\table.txt", table);

            Console.WriteLine("\nFind the output at table.txt");
        }
开发者ID:iliankostov,项目名称:DatabaseApplications,代码行数:58,代码来源:PlayWithSelect.cs

示例3: Benchmark2

        public void Benchmark2()
        {
            var sw = new Stopwatch();
            var times = new TimeSpan[10];

            for (var i = 0; i < 10; i++)
            {
                sw.Start();

                var allFriends = this.db.Views.QueryAsync<Person>(new QueryViewRequest("person", "all_friends")).Result;

                var randomPerson = allFriends.Rows.RandomEntry(this.rng).Value;

                var friendsFromRandom = this.RelationsFrom(randomPerson, "Friend").ToList();

                var friendsFromFriendsFromRandom = friendsFromRandom.SelectMany(x => this.RelationsFrom(x, "Friend")).ToList();

                Console.WriteLine("Count results : " + friendsFromFriendsFromRandom.Count);
                times[i] = sw.Elapsed;
                sw.Stop();
                sw.Reset();
            }
            Console.WriteLine("=====================================================================");
            Console.WriteLine("Min time : " + times.Min());
            Console.WriteLine("Max time : " + times.Max());
            Console.WriteLine("Average time : " + TimeSpan.FromTicks((long)times.Average(x => x.Ticks)));
            Console.WriteLine("Median time : " + times.OrderBy(x => x).ElementAt(times.Length / 2));
            Console.WriteLine("=====================================================================");
        }
开发者ID:Uwy,项目名称:TpNoSQL-CouchDB,代码行数:29,代码来源:CouchDBBenchmark.cs

示例4: Main

        public static void Main()
        {
            var context = new AdsEntities();
            //// Initialize connection
            var count = context.Ads.Count();
            TimeSpan[] nonOptimizedArray = new TimeSpan[6];
            TimeSpan[] optimizedArray = new TimeSpan[6];
            Stopwatch sw = new Stopwatch();
            sw.Start();

            for (int i = 0; i < 6; i++)
            {
                sw.Restart();
                var adsBadWay = context.Ads
                    .ToList()
                    .Where(a => a.AdStatus.Status == "Published")
                    .Select(
                        a => new
                        {
                            a.Title,
                            a.Category,
                            a.Town,
                            a.Date
                        })
                    .ToList()
                    .OrderBy(a => a.Date).Count();
                nonOptimizedArray[i] = sw.Elapsed;

                sw.Restart();
                var adsOptimized = context.Ads
                    .Where(a => a.AdStatus.Status == "Published")
                    .OrderBy(a => a.Date)
                    .Select(a => new
                    {
                        a.Title,
                        a.Category,
                        a.Town,
                        a.Date
                    }).Count();
                optimizedArray[i] = sw.Elapsed;

                if (i == 5)
                {
                    Console.WriteLine("Number of elements for non optimized query: " + adsBadWay);
                    Console.WriteLine("Number of elements for optimized query: " + adsOptimized);
                }
            }

            sw.Stop();

            string table = string.Format(
            @"
                       Run 1              Run 2              Run 3              Run 4              Run 5              Run 6         Average time (ms)
             --------------- ------------------ ------------------ ------------------ ------------------ ------------------ ------------------ -------------------
              Non-optimized   {0}   {1}   {2}   {3}   {4}   {5}   {6:F10}
              Optimized       {7}   {8}   {9}   {10}   {11}   {12}   {13:F10}
            ",
            nonOptimizedArray[0],
            nonOptimizedArray[1],
            nonOptimizedArray[2],
            nonOptimizedArray[3],
            nonOptimizedArray[4],
            nonOptimizedArray[5],
            nonOptimizedArray.Average(n => n.Milliseconds),
            optimizedArray[0],
            optimizedArray[1],
            optimizedArray[2],
            optimizedArray[3],
            optimizedArray[4],
            optimizedArray[5],
            optimizedArray.Average(o => o.Milliseconds));
            File.WriteAllText(@"..\..\table.txt", table);

            Console.WriteLine("Find the output at table.txt");
        }
开发者ID:iliankostov,项目名称:DatabaseApplications,代码行数:75,代码来源:PlayWithToList.cs

示例5: Stats

            public Stats(string name, int count, TimeSpan[] times, int exceptionsCount)
            {
                Array.Sort(times, 0, count);

                Min = times[0];
                Med = times[count / 2];
                Max = times[count - 1];
                Avg = new TimeSpan((int)times.Average(_ => _.Ticks));
                ExceptionsCount = exceptionsCount;
                Name = name;
            }
开发者ID:JoshKeegan,项目名称:DataStructures,代码行数:11,代码来源:ExecWithStats.cs


注:本文中的System.TimeSpan.Average方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。