Reputation: 11389
I have the line
double A =uJoinFeatures1.Values [i];
This creates a copy of the double value
struct udtJoinFeatures
{
double Values[17];
};
But for speed reasons, I would simply like to reference the value without copying it. How would I do that, please?
I tried
double A =&uJoinFeatures1.Values [i];
but that was not possible.
Upvotes: 0
Views: 1036
Reputation: 76246
For speed reasons especially, by all means COPY!
Indirect access is likely to cost more than copying few bytes, especially since it makes it harder for the compiler to keep the value in registers (it would have to optimize the reference away first; references can only point to memory).
In any case it is definitely a premature optimization. After you've made sure all your algorithms have optimal complexity, minimized allocations, work duplications and all other algorithmic problems, you should ensure that the inner loops are inlined and than you'll find that the compiler will do the best possible thing with the normal copy variant anyway.
Upvotes: 1
Reputation: 409166
You declare A
as a reference to a double
:
double& A = ...;
In this case though, it really is not going to be any speed increase. On a 64-bit machine both a double
and a pointer (which references really are) are both 64 bits, the same amount to copy. And because references are actually just another name for pointers, you have the indirection when you access the reference which probably adds more instructions than using just a double
.
Not that it's going to be measurable anyway, unless you do this for many millions of items in a tight loop.
Upvotes: 5