7
I have a project that works with a large volume of data, and I need to optimize it to bring results of some calculations in a considerably small time. I know that I have several aspects to take into consideration, such as the structure in which the data was saved in the database, the way I am doing access to them, how I am performing the calculations among others, but disregarding all these items, I would like to take into consideration only the question raised below.
My doubt is more conceptual than a problem in my code. But something specific...
Consider the following list:
var minhaLista = new List<MeuObjeto>
{
// Objetos...
};
meuObjeto
has the following properties:
public class MeuObjeto
{
public int Prop1 {get; set;}
public string Prop2 {get; set;}
public decimal Prop3 {get; set;}
public bool Prop4 {get; set;}
}
I need to access each of the properties in a loop with n
items as quickly as possible and economically in relation to memory. But if I have to choose between speed and memory, I must choose speed.
Every millisecond is very important, so I’m taking into consideration some aspects like for
be faster than the foreach
, or declare a constant with 0
and uses it to instantiate the loop control variable to be better than instantiating directly with 0
.
So consider INICIO
as follows:
private const int INICIO = 0;
Consider OutroObjeto
as an object similar to MeuObjeto
for example only.
Form 1:
var outraLista = new List<OutroObjeto>();
for (int i = INICIO; i < minhaLista.Count; i++)
{
var outroObjeto = new OutroObjeto
{
Prop1 = minhaLista[i].Prop1,
Prop2 = minhaLista[i].Prop2,
Prop3 = minhaLista[i].Prop3,
Prop4 = minhaLista[i].Prop4
};
outraLista.Add(outroObjeto );
}
In this case, for each property a search is made on the list by object in position
i
?
Form 2:
var outraLista = new List<OutroObjeto>();
for (int i = INICIO; i < minhaLista.Count; i++)
{
var meuObjetoI = minhaLista[i];
var outroObjeto = new OutroObjeto
{
Prop1 = meuObjetoI.Prop1,
Prop2 = meuObjetoI.Prop2,
Prop3 = meuObjetoI.Prop3,
Prop4 = meuObjetoI.Prop4
};
outraLista.Add(outroObjeto );
}
Apparently this section works in a similar way to
foreach
, but access to each property of the object in positioni
of the list will be faster than in Form 1?Technically
meuObjetoI
only points to the list object in the positioni
which is already allocated in memory, correct?
What would be the most suitable way taking into account the time and the memory consumption?
Or is there a third option that’s better?
How are you getting to
minhaLista
?– Randrade
@Randrade, thank you so much for your interest in helping, but I’d like you to consider
minhaLista
as a common list ofn
MeuObjeto
, and only take into account access to them in the forms presented. Is that possible or do you really need that detail to formulate a response?– Jedaias Rodrigues
Is that usually the best way to do this could be at the source, where you’re getting the data originally.
– Randrade
Youthful, install this.
– Leonel Sanches da Silva