Advertisement
Guest User

Untitled

a guest
Jan 12th, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02.Performance_of_Operations
  6. {
  7.     using System.Diagnostics;
  8.  
  9.     class CalculateTimes
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             TimeSpan averageAddTimeOperation = GetAddOperationAverageTime();
  14.  
  15.             Console.WriteLine(averageAddTimeOperation);
  16.         }
  17.  
  18.         private static TimeSpan GetAddOperationAverageTime()
  19.         {
  20.             Stopwatch timer = new Stopwatch();
  21.  
  22.             List<TimeSpan> times = new List<TimeSpan>();
  23.  
  24.             for (int i = 0; i < 500; i++)
  25.             {
  26.                 timer.Restart();
  27.                 timer.Start();
  28.                 var a = 0;
  29.                 for (int j = 0; j < 5000; j++)
  30.                 {
  31.                     a = a + 500;
  32.                 }
  33.                 timer.Stop();
  34.                 times.Add(timer.Elapsed);
  35.             }
  36.  
  37.             double averageTicks = times.Average(timespan => timespan.Ticks);
  38.             TimeSpan average = new TimeSpan(Convert.ToInt64(averageTicks));
  39.  
  40.             return average;
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement