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 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 accessing them, how I am performing the calculations among others, but disregarding all these items, I would like be taken into account only the question posed below.
My question 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 of% items as quickly and economically as possible in memory. But if I have to choose between speed and memory, I should choose speed.
Every millisecond is very important, so I'm taking into account some aspects like n
being faster than for
, or else declaring a constant with foreach
and using it to instantiate the control variable of the loop is better than instantiating directly with 0
.
So, consider 0
as follows:
private const int INICIO = 0;
Consider INICIO
as an object similar to OutroObjeto
just for example.
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 in the list is made by object at position
MeuObjeto
?
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 snippet works similarly to
i
, but access to each property of the object atforeach
of the list will it be faster than in Form 1 ?Technically
i
only points to the list object in positionmeuObjetoI
that is already allocated in memory, correct?
What would be the most appropriate way of taking the time and memory consumption?
Or is there a third option that is better?