Which is more efficient Cstr(value) or value.ToString()

前端 未结 9 951
我在风中等你
我在风中等你 2021-01-11 14:56

I am wondering which is more efficient, using CStr() or object.toString(). The reason I ask this is because I though all that CStr() done was to invoke the .ToString() metho

9条回答
  •  借酒劲吻你
    2021-01-11 15:36

    I tried to time the following two subroutines (forgive my variable naming):

     For i As Integer = 1 To 1000
            For j As Integer = 1 To 65536
                eee = aaa.ToString()
                eee = bbb.ToString()
                eee = ccc.ToString()
                eee = ffffd.ToString()
                eee = Nothing
            Next
     Next
    

    and

        For i As Integer = 1 To 1000
            For j As Integer = 1 To 65536
                eee = CStr(aaa)
                eee = CStr(bbb)
                eee = CStr(ccc)
                eee = CStr(ffffd)
                eee = Nothing
            Next
        Next
    

    where

        Dim aaa As Integer = 1233
        Dim bbb As Single = 445.234234234
        Dim ccc As Double = 234234.234457634
        Dim ffffd As Decimal = 1231.3466734523424
        Dim eee As String = Nothing
    

    the ToString() loop takes 62 seconds and the CStr() loop takes 64 seconds. It is really not a huge impact in my opinion, if you are only dealing with numbers. I disabled any compiler optimization during the timing process.

提交回复
热议问题