Performance between creating multiple objects or just one

Asked

Viewed 85 times

0

I always had a question regarding the code blocks below, which would behave faster taking into account the service:

foreach(var t in listaT)
{  
 using(TService service = new Tservice())
 { 
    t = service.Find(t.Id)
    t.ValorPontuacao = Math.Round(Convert.ToDecimal((t.ValorR / t.Valor) * t.ValorP), 2, MidpointRounding.ToEven);
    listT.Add(t);
 };  
} 

using(TService service = new Tservice())
{
 foreach(var t in listaT)
 { 
  t = service.Find(t.Id);  
  t.ValorPontuacao = Math.Round(Convert.ToDecimal((t.ValorR / t.Valor) * t.ValorP), 2, MidpointRounding.ToEven);
   listT.Add(t);
 }
}

The excerpt of code is only one example, I would like to know which is more performative, knowing that the second example would give "less" work for Garbage Collector. Making it clear that the TService is an overload of AbstractService<TDAO,T>, it is worth noting that the class T is only an example and the list will be manipulated later.

2 answers

1

It depends on a number of questions, can not answer without knowing the implementation details of each thing.

In fact it is likely that the second is more performative because it not only does not do several instantiations, but also generates fewer objects and therefore less pressure to the Garbage Collector therefore it will generate less pauses and less work for GC. When more object is created in the heap worse for GC. The first seems to create a single object.

If it wasn’t clear, every new generates a new object that will be discarded soon after. I just can’t say whether you need to create these objects or not, each object can have a different semantics. If an object is only possible I wonder if it would really need create an object or it should be static, another possible error of this code.

There’s a slight chance of that TService be a struct That might change that explanation a little bit because it doesn’t put pressure on the GC, but it would still be slightly worse in the first case. And nothing indicates that it would be this, but it might, after all it might be that neither class and struct seems to be necessary there. Of course, I am speculating, the question is not clear to state about this.

Nor am I going to go into the merit that is altering the object of the loop iterator because this is another issue and I imagine that the code was done just to exemplify, but it’s a bad example, which does nothing useful, so ultimately, both are pretty bad for equal, even if one of them manages more effort. So it doesn’t do much to understand a code error if it has several others.

The editing shows that the code makes no sense and should not do any of this, so the problem is not even the performance.

  • I understand, sorry for exemplifying a bad code. I will add more details in the code to improve the question.

0


Analyzing this code by simply looking at as you’re doing it, without looking what you are doing, in a very pragmatic way, in very few scenarios the first would be better than the second, taking into account that the first will do N instantiations of the same object, generating more work for the GC for nothing.

Now looking at the what you’re doing, there would be several bottlenecks, as fact of you being doing a find that probably makes a query to the bank for each T in the list, you could make a specialized query in the service that would expect a list of Ids, and return a list of t. The fact that you’re giving new in a service, already shows that you are not using Dependency Injection that would already solve object construction problems in your application.

Browser other questions tagged

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