K
kid joe
Hi all,
I know this is fairly system specific but I was hoping some people here
with a wide experience of C implementations on different platforms could
pass on their knowledge.
If I want to perform actions on several large arrays, whats the best way
to do that? Lets say for a silly example I have arrays a,b,c all of size n
and I want to multiply everything in a by 2, everything in b by 3 and
everything in c by 4.
Method 1:
for(long int i = 0; i < n; i++) {
a *= 2;
b *= 3;
c *= 4;
}
Method 2:
for(long int i = 0; i < n; i++)
a *= 2;
for(long int i = 0; i < n; i++)
b *= 3;
for(long int i = 0; i < n; i++)
c *= 4;
Now you could argue that Method 1 should be faster because there are less
branching instructions, and branching is expensive. But then you could
argue that Method 2 should be better because theres better data locality
so better use of cache.
I know people will say to profile it on my system!
But Id be interested
in peoples thoughts on this on a variety of different hardwares.
Cheers,
Joe
I know this is fairly system specific but I was hoping some people here
with a wide experience of C implementations on different platforms could
pass on their knowledge.
If I want to perform actions on several large arrays, whats the best way
to do that? Lets say for a silly example I have arrays a,b,c all of size n
and I want to multiply everything in a by 2, everything in b by 3 and
everything in c by 4.
Method 1:
for(long int i = 0; i < n; i++) {
a *= 2;
b *= 3;
c *= 4;
}
Method 2:
for(long int i = 0; i < n; i++)
a *= 2;
for(long int i = 0; i < n; i++)
b *= 3;
for(long int i = 0; i < n; i++)
c *= 4;
Now you could argue that Method 1 should be faster because there are less
branching instructions, and branching is expensive. But then you could
argue that Method 2 should be better because theres better data locality
so better use of cache.
I know people will say to profile it on my system!
in peoples thoughts on this on a variety of different hardwares.
Cheers,
Joe