fork download
  1. Imports System
  2. Imports System.Diagnostics
  3. Imports System.Collections.Generic
  4.  
  5. Public Class Test
  6. Public Shared Sub Main()
  7. Dim abHeader As Byte() = New Byte() {255, 127, 0, 0}
  8. Dim Format As String = "{0,-24}: {1}" & Environment.NewLine & "{2,-24}: {3}"
  9. Dim sw As New Stopwatch
  10. Dim r As New Random
  11.  
  12. sw.Restart()
  13. Dim Result1 As Long = Method1(abHeader)
  14. sw.Stop()
  15. Dim Time1 As TimeSpan = sw.Elapsed
  16.  
  17. 'sw.Restart()
  18. 'Dim Result2 As Long = Method2(abHeader)
  19. 'sw.Stop()
  20. 'Dim Time2 As TimeSpan = sw.Elapsed
  21.  
  22. sw.Restart()
  23. Dim Result3 As Long = Method3(abHeader)
  24. sw.Stop()
  25. Dim Time3 As TimeSpan = sw.Elapsed
  26.  
  27. Console.WriteLine(Format, "Bitwise Or", Result1, "Compile time", Time1)
  28.  
  29. Dim ExecutionTimes1 As New List(Of Long)
  30. For x As Integer = 1 To 1000000
  31. abHeader = New Byte(0 To 3) {r.Next(0, 255), r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)}
  32. sw.Restart()
  33. Dim Result As Long = Method1(abHeader)
  34. sw.Stop()
  35. ExecutionTimes1.Add(sw.Elapsed.Ticks)
  36. Next
  37. Console.WriteLine("{0,-24}: {1}", "1M iterations avg.", TimeSpan.FromTicks(GetAverage(ExecutionTimes1)))
  38. Console.WriteLine()
  39.  
  40. Console.WriteLine(Format, "Convert.ToInt64()", "<CAST ERROR>", "Compile time", "???")
  41. Console.WriteLine()
  42.  
  43. Console.WriteLine(Format, "BitConverter.ToInt32()", Result3, "Compile time", Time3)
  44.  
  45. Dim ExecutionTimes3 As New List(Of Long)
  46. For x As Integer = 1 To 1000000
  47. abHeader = New Byte(0 To 3) {r.Next(0, 255), r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)}
  48. sw.Restart()
  49. Dim Result As Long = Method3(abHeader)
  50. sw.Stop()
  51. ExecutionTimes3.Add(sw.Elapsed.Ticks)
  52. Next
  53. Console.WriteLine("{0,-24}: {1}", "1M iterations avg.", TimeSpan.FromTicks(GetAverage(ExecutionTimes3)))
  54. End Sub
  55.  
  56. Public Shared Function Method1(ByVal Array As Byte()) As Long
  57. 'NOTE: Indexes reversed due to different compiler endianness.'
  58. Return (CLng(Array(3)) << 24) Or (CLng(Array(2)) << 16) Or (CLng(Array(1)) << 8) Or (CLng(Array(0)))
  59. End Function
  60.  
  61. Public Shared Function Method2(ByVal Array As Byte()) As Long
  62. Return Convert.ToInt64(Array)
  63. End Function
  64.  
  65. Public Shared Function Method3(ByVal Array As Byte()) As Long
  66. Return CLng(BitConverter.ToInt32(Array, 0))
  67. End Function
  68.  
  69. Public Shared Function GetAverage(ByVal List As List(Of Long)) As Long
  70. Dim ReturnValue As Long = 0
  71. For Each Value As Long In List
  72. ReturnValue += Value
  73. Next
  74. Return ReturnValue / List.Count
  75. End Function
  76. End Class
Success #stdin #stdout 1.53s 24320KB
stdin
Standard input is empty
stdout
Bitwise Or              : 32767
Compile time            : 00:00:00.0000725
1M iterations avg.      : 00:00:00.0000003

Convert.ToInt64()       : <CAST ERROR>
Compile time            : ???

BitConverter.ToInt32()  : 32767
Compile time            : 00:00:00.0001834
1M iterations avg.      : 00:00:00.0000003