Reputation: 6194
Imagine that I have a struct that contains a set of variables that describe an object, which is a grid in my case. I was wondering, if I have a function that only uses a subset of the grid, whether there are any performance differences in the two variants of the computational_kernel
functions below. The kernels are the same, except that the one in which the struct is passed has to extract the itot
, jtot
and ktot
from the struct before heavy computations are done.
struct Grid
{
int itot;
int jtot;
int ktot;
int not_used_in_kernel1;
int not_used_in_kernel2;
int not_used_in_kernel3;
int not_used_in_kernel4;
}
Grid grid;
// Code that initializes the grid values...
// Variant 1
computational_kernel(double* array1, double* array2,
const int itot, const int jtot, const int ktot);
// Variant 2
computational_kernel(double* array1, double* array2,
const Grid& grid);
Upvotes: 0
Views: 364
Reputation: 2540
If computational_kernel
is a function that does a lot of work internally and is invoked few times the difference between the two versions is infinitesimal. The second version has just the extra cost of dereferencing 3 values, than the rest is identical, and you presumably have to do such dereferencing anyway before invoking the first version.
I would definitely use the second form for compactness reasons: if you are defining object-oriented data structures then use them in such a fashion (better encapsulation).
Upvotes: 1
Reputation: 4958
I would say that passing a reference to a struct as in the 2nd variant would probably be more efficient performance-wise. On the 1st variant the caller will need to push the 3 int variables on the stack while on the 2nd variant all it has to push is a reference (a pointer) to the struct and do the stuff there. The performance impact is of course bigger if you had more than 3 variables to pass.
Upvotes: 0
Reputation: 81
I think passing a struct is better for the code maintenence. if you add new fields to your grid, you will only have to change the function. But passing a set of variables you will have to change the function and every call to the function.
Upvotes: 2