Reboot a variable, or re-create it?

1

In terms of performance, which is more preferable? Are there other differences between the two modes, other than performance?

Reboot a variable multiple times?

Private Sub Metodo()
    Dim MeuTipo As Tipo

    For i As Integer = 0 To 100
        MeuTipo = New Tipo
        MeuTipo.FacaAlgumaCoisa()
    Next
End Sub

Or recreate it multiple times?

Private Sub Metodo()    
    For i As Integer = 0 To 100
        Dim MeuTipo As New Tipo
        MeuTipo.FacaAlgumaCoisa()
    Next
End Sub
    
asked by anonymous 20.12.2016 / 22:42

1 answer

1

I have generated the IL code of both and got exactly the same code, so there is no difference between them. There is a question about C # that talks more or less about it .

  .method private static void  Metodo() cil managed
  {
    // 
    .maxstack  2
    .locals init (class Tipo V_0,
             int32 V_1,
             int32 V_2,
             bool V_3)
    IL_0000:  nop
    IL_0001:  ldc.i4.0
    IL_0002:  stloc.2
    IL_0003:  newobj     instance void Tipo::.ctor()
    IL_0008:  stloc.0
    IL_0009:  ldloc.0
    IL_000a:  callvirt   instance void Tipo::FacaAlgumaCoisa()
    IL_000f:  nop
    IL_0010:  ldloc.2
    IL_0011:  ldc.i4.1
    IL_0012:  add.ovf
    IL_0013:  stloc.2
    IL_0014:  ldloc.2
    IL_0015:  ldc.i4.s   100
    IL_0017:  cgt
    IL_0019:  ldc.i4.0
    IL_001a:  ceq
    IL_001c:  stloc.3
    IL_001d:  ldloc.3
    IL_001e:  brtrue.s   IL_0003

    IL_0020:  ret
  } // end of method Module1::Metodo

  .method private static void  Metodo2() cil managed
  {
    // 
    .maxstack  2
    .locals init (int32 V_0,
             int32 V_1,
             class Tipo V_2,
             bool V_3)
    IL_0000:  nop
    IL_0001:  ldc.i4.0
    IL_0002:  stloc.1
    IL_0003:  newobj     instance void Tipo::.ctor()
    IL_0008:  stloc.2
    IL_0009:  ldloc.2
    IL_000a:  callvirt   instance void Tipo::FacaAlgumaCoisa()
    IL_000f:  nop
    IL_0010:  ldloc.1
    IL_0011:  ldc.i4.1
    IL_0012:  add.ovf
    IL_0013:  stloc.1
    IL_0014:  ldloc.1
    IL_0015:  ldc.i4.s   100
    IL_0017:  cgt
    IL_0019:  ldc.i4.0
    IL_001a:  ceq
    IL_001c:  stloc.3
    IL_001d:  ldloc.3
    IL_001e:  brtrue.s   IL_0003

    IL_0020:  ret
  } // end of method Module1::Metodo2

See in dotNetFiddle .

    
21.12.2016 / 00:04