1: Dim max As Integer = 1000000
2: Dim sw As New System.Diagnostics.Stopwatch
3: Dim duration As TimeSpan
4: sw.Start()
5: For index As Integer = 1 To max
6: Dim MyObject As Object = "Hello World"
7: Dim MyString1 As String = CType(MyObject, String)
8: Next
9: duration = New TimeSpan(sw.ElapsedTicks)
10: Console.WriteLine("CType Value Type to Object:{0:0.00000}", duration.TotalMilliseconds)
11: sw.Reset()
12:
13: sw.Start()
14: For index As Integer = 1 To max
15: Dim MyObject As Object = "Hello World"
16: Dim MyString1 As String = DirectCast(MyObject, String)
17: Next
18: duration = New TimeSpan(sw.ElapsedTicks)
19: Console.WriteLine("DirectCast Value Type to Object:{0:0.00000}", duration.TotalMilliseconds)
20: sw.Reset()
21:
22: sw.Start()
23: For index As Integer = 1 To max
24: Dim MyObject As Object = "Hello World"
25: Dim MyString1 As String = TryCast(MyObject, String)
26: Next
27: duration = New TimeSpan(sw.ElapsedTicks)
28: Console.WriteLine("TryCast Value Type to Object:{0:0.00000}", duration.TotalMilliseconds)
29: sw.Reset()
30:
31:
32: sw.Start()
33: For index As Integer = 1 To max
34: Dim MyObject As New Cat
35: Dim MyString1 As Cat = CType(MyObject, Cat)
36: Next
37: duration = New TimeSpan(sw.ElapsedTicks)
38: Console.WriteLine("CType Ref type to Object:{0:0.00000}", duration.TotalMilliseconds)
39: sw.Reset()
40:
41: sw.Start()
42: For index As Integer = 1 To max
43: Dim MyObject As New Cat
44: Dim MyString1 As Cat = DirectCast(MyObject, Cat)
45: Next
46: duration = New TimeSpan(sw.ElapsedTicks)
47: Console.WriteLine("DirectCast Ref type to Object:{0:0.00000}", duration.TotalMilliseconds)
48: sw.Reset()
49:
50: sw.Start()
51: For index As Integer = 1 To max
52: Dim MyObject As New Cat
53: Dim MyString1 As Cat = TryCast(MyObject, Cat)
54: Next
55: duration = New TimeSpan(sw.ElapsedTicks)
56: Console.WriteLine("TryCast Ref type to Object:{0:0.00000}", duration.TotalMilliseconds)