Current data structure
I have a code in which the data structure is represented by two vectors of classes A1
and A2
respectively.
vector<A1> v1;
vector<A2> v2;
The slow part of my code (about 80% of the total runtime) consists at iterating over these vectors and applying the method funA1
on elements of v1
and funA2
on elements of v2
.
double result = 0;
for (int i = 0 ; i < v1.size() ; i++)
{
results *= v1[i].funA1();
}
for (int i = 0 ; i < v2.size() ; i++)
{
results *= v2[i].funA2();
}
Reshaping the data structure
While the data structure uses these two vectors, the intuition would rather to using a single vector that mixes the data type A1
and A2
. Using two different vectors brings a loss of readability and maintenance elsewhere in the code.
I thought I could merge them into a single vector. The idea would be to make A1
and A2
siblings classes of a parent class A
and use vector<A> v
as single vector. I would define a virtual fun
in the class A
and override it in both A1
and A2
so that I could do
double result = 0;
for (int i = 0 ; i < v.size() ; i++)
{
results *= v[i].fun();
}
Question
In the first case, the methods funA1
and funA2
can be inlined by the compiler (in fact I inlined them myself as they are very simple functions).
In the second case I am afraid inlining would not be possible. Is this correct?
Will the compiler manage to inline the method fun
?
Of course the loss of performance might be negligible and I should test it but I would like to know whether it seems a priori worth it before trying to make all the modifications.