Time running speed with Stopwatch - CSharp Operating System

CSharp examples for Operating System:Diagnostics

Description

Time running speed with Stopwatch

Demo Code

using System;//from   w  ww .  j a v a  2  s  .c o  m
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using static System.Console;
using static System.Diagnostics.Process;
class Program
{
   static void  Main(string[] args)
   {
      Recorder.Start();
      int[] largeArrayOfInts = Enumerable.Range(1, 10000).ToArray();
      Write("Press ENTER to stop the timer: ");
      ReaderLine();
      Recorder.Stop();
   }
}
public static class Recorder
{
   static Stopwatch timer = new Stopwatch();
   static long bytesPhysicalBefore = 0;
   static long bytesVirtualBefore = 0;
   public static void Start()
   {
      GC.Collect();
      GC.WaitForPendingFinalizers();
      GC.Collect();
      bytesPhysicalBefore = GetCurrentProcess().WorkingSet64;
      bytesVirtualBefore = GetCurrentProcess().VirtualMemorySize64;
      timer.Restart();
   }
   public static void Stop()
   {
      timer.Stop();
      long bytesPhysicalAfter = GetCurrentProcess().WorkingSet64;
      long bytesVirtualAfter = GetCurrentProcess().VirtualMemorySize64;
      WriteLine("Stopped recording.");
      WriteLine($"{bytesPhysicalAfter - bytesPhysicalBefore:N0} physical bytes used.");
      WriteLine($"{bytesVirtualAfter - bytesVirtualBefore:N0} virtual bytes used.");
      WriteLine($"{timer.Elapsed} time span ellapsed.");
      WriteLine($"{timer.ElapsedMilliseconds:N0} total milliseconds ellapsed.");
   }
}

Related Tutorials