本文整理汇总了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));
}
示例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");
}
示例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("=====================================================================");
}
示例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");
}
示例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;
}