VB.NET程序运行耗时精确计量方法之一

前端之家收集整理的这篇文章主要介绍了VB.NET程序运行耗时精确计量方法之一前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. Imports System.Threading
  2. '精确计量程序运行时间的STOPWATCH
  3. Module Module1
  4. Sub Main1()
  5. ' Create new Stopwatch instance.
  6. Dim watch As New Diagnostics.Stopwatch
  7. watch.Start() '启动
  8.  
  9. ' Measure.
  10. For i As Integer = 0 To 1000 - 1
  11. Threading.Thread.Sleep(1)
  12. Next
  13.  
  14. ' 停止并显示时间
  15. watch.Stop()
  16. Console.WriteLine(watch.Elapsed.TotalMilliseconds)
  17.  
  18. ' This isn't measured.
  19. For i As Integer = 0 To 1000 - 1
  20. Threading.Thread.Sleep(1)
  21. Next
  22.  
  23. ' Begin measuring again.
  24. watch.Start()
  25.  
  26. ' Measure.
  27. For i As Integer = 0 To 1000 - 1
  28. Threading.Thread.Sleep(1)
  29. Next
  30.  
  31. ' Stop measuring again (not always needed).
  32. watch.Stop()
  33. Console.WriteLine(watch.Elapsed.TotalMilliseconds)
  34.  
  35. Console.ReadLine()
  36. End Sub
  37.  
  38.  
  39. Sub Main2()
  40. ' Create a Stopwatch and sleep for zero milliseconds.
  41. Dim stopwatch As New Diagnostics.Stopwatch ' = stopwatch.StartNew
  42. stopwatch.Start()
  43.  
  44. Thread.Sleep(0)
  45. stopwatch.Stop()
  46.  
  47. ' Write the current time.
  48. Console.WriteLine(stopwatch.ElapsedMilliseconds)
  49. Console.WriteLine(DateTime.Now.ToLongTimeString)
  50.  
  51. ' Start a new Stopwatch.
  52. stopwatch = stopwatch.StartNew
  53. Thread.Sleep(5000)
  54. stopwatch.Stop()
  55. Console.WriteLine(stopwatch.ElapsedMilliseconds)
  56. Console.WriteLine(DateTime.Now.ToLongTimeString)
  57.  
  58. ' Start a new Stopwatch.
  59. stopwatch = stopwatch.StartNew
  60. Thread.Sleep(1000)
  61. stopwatch.Stop()
  62. Console.WriteLine(stopwatch.ElapsedMilliseconds)
  63.  
  64. ' Start a new Stopwatch and use SpinWait.
  65. stopwatch = stopwatch.StartNew
  66. Thread.SpinWait(1000000000)
  67. stopwatch.Stop()
  68. Console.WriteLine(stopwatch.ElapsedMilliseconds)
  69.  
  70. Console.ReadLine()
  71. End Sub
  72.  
  73.  
  74. Sub Main()
  75. 'Dim sw As New Diagnostics.Stopwatch
  76. 'sw.Start()
  77.  
  78. 'For i As Integer = 0 To 100000
  79. ' For k As Integer = 0 To 100000
  80.  
  81. ' Next
  82. 'Next
  83. 'sw.Stop()
  84. 'Console.WriteLine(sw.ElapsedMilliseconds)
  85. 'Console.ReadLine()
  86.  
  87.  
  88. End Sub
  89.  
  90. End Module
  91.  

猜你在找的VB相关文章