Wednesday, 6 May 2009

Get-ElapsedTime.ps1

  1. <#  
  2. .SYNOPSIS  
  3.     Demonstrates use of the System.Diagnostics.Stopwatch .NET Class    
  4. .DESCRIPTION  
  5.     This script is a community content MSDN sample,using PowerShell  
  6. .NOTES  
  7.     File Name  : Get-ElapsedTime 
  8.     Author     : Thomas Lee - tfl@psp.co.uk  
  9.     Requires   : PowerShell V2 CTP3  
  10. .LINK  
  11.     Sample posted to:  
  12.     http://pshscripts.blogspot.com  
  13.     Original MSDN sample at:  
  14.     http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx 
  15. .EXAMPLE  
  16.     PSH [C:\foo]: .\get-stopwatch.ps1' 
  17.     Runtime = 00:00:10.03 
  18.  
  19. #>  
  20.  
  21. ## 
  22. # start of script 
  23. ## 
  24.  
  25. # Create a new stopwatch objecty 
  26. $StopWatch = New-Object system.Diagnostics.Stopwatch 
  27.   
  28. # Start the stop watch 
  29. $stopWatch.Start() 
  30.  
  31. # Go to sleep for approx 10 seconds 
  32. [system.threading.Thread]::Sleep(10000) 
  33.  
  34. # Now after sleep, stop the watch 
  35. $StopWatch.Stop(); 
  36.  
  37. # Get the elapsed time as a TimeSpan value. 
  38. $ts = $StopWatch.Elapsed 
  39.  
  40. # Format and display the TimeSpan value. 
  41. $ElapsedTime = [system.String]::Format("{0:00}:{1:00}:{2:00}.{3:00}"
  42.            $ts.Hours, $ts.Minutes, $ts.Seconds, 
  43.             $ts.Milliseconds / 10); 
  44. "Runtime = $elapsedTime" 

1 comment:

Unknown said...

Is Stopwatch better than DateTime::Now differences?