Reset a variable, or recreate it?

Asked

Viewed 72 times

2

In terms of performance, what is more preferable? There are other differences between the two modes than performance?

Reset 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
  • 2

    The two forms are bad, because MeuTipo could be instantiated once out of the for and within it call FacaAlgumaCoisa()! the two forms create unnecessary instances to each interaction.

  • I understand. And what is the disadvantage of having unnecessary instances at each iteration?

  • Look in this specific case of the example of the code presented, there is first the need to have an object to each interaction already seen that an Object of the class MeuTipo provides the method FacaAlgumaCoisa() which is what you need to call in every interaction of the repeat structure. You may even ask me more is there at some point that I can use. Yes, there is an example that I remember now is when we need to create a List of Objects by Example of Meutipo, the first code of yours with an adjustments would give to employ this. In the context it is not necessary only use memory

1 answer

2


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

  .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

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.