7 Answers
7

There is unlikely to be any difference whatsoever. The memory layout will be exactly the same with both class structures; as such, most of the generated code will be the same as well (and, consequently, the performance will be the same).

The only difference I can see between the two options is that you can index into the floats if they're stored as an array. Depending on how you plan on using them, this may or may not be useful.

This smells strongly of premature optimization. Instead, name the floats meaningfully in relation to their purpose. Are they four unrelated value? Make them individual attributes. Are they four sequenced values? Make it an array. The actual performance different should be negligible if at all.

I've read somewhere that compilers tend to have an optimization routine that inlines all iterations of fixed length <= 4. In this light it does not really matter how you store them.
You ask about "parameter passing". In that case, if you'd like to pass them all together to some routine then it is handier to have them all in a fixed size array. If you make them an array you can always add some getters/setters to both the array as a whole and to each value.

As much as I know, accessing 4 floats in an array is faster than 4 instance variables declared in the class.

The code generated by C++ compilers for arrays uses indexed addressing mode which is easier and faster for intel processors.

I've worked on OpenGL for 3D graphics a long time ago. The functions were either able to take a vertex either as x, y, z values, vertex structures or an array of float with 3 members. The functions which required arrays were faster than their counterparts.

The speed would also depends on what kind of calculations would you be performing.

You haven't mentioned much about the design or domain but also check also if it makes more sense to store them in an array or instance variable. Remember that optimized code comes at the price of reduced readability and sometimes maintenance issues.

I compiled code using Apple LLVM 5.0 that accessed a struct with an array of four float using x->f[2] and other code that accessed a struct with four separate members using x->c. In both cases, the generated assembly code used 8(%rdi) to refer to the member. In fact, the implementation of code that accesses individual members is inherently a subset of code that accesses array elements, since any reference to .MemberName in a struct of individual members is equivalent to some .Array[Index] in a struct with an array. (The converse is not true since an index may be non-constant.)
–
Eric PostpischilSep 9 '13 at 23:51

Not every compiler generates the exact same instructions and performs same optimizations. "All compilers are not equal" otherwise how is it possible to have a faster exe from an intel compiler than VC and C++ builder compiler from the same code?
–
Simple FellowSep 13 '13 at 13:08

@Hymir I read your question again and my answer. I missed teh last line "Especially in combination with stl-containers and parameter passing" The simple answer is Yes, they are faster when passed as parameter and stl container can handle them well. since its a comment i cannot write details here but "Yes" is the answer. Now please mark mark my answer as the best answer! :)
–
Simple FellowSep 13 '13 at 13:26

Formally it depends on the compiler, but speed and size should be the same.

A global array indexed with a constant is for the compiler exactly the same thing as a global variable: both of them translate to just a static address in memory.

A member array indexed with a constant is exactly the same thing as a member variable: both of them translate to a an address at a fixed offset from this.

Also g++ for sure (but most probably other compilers too) are able to merge a copy of multiple adjacent variables with a single memory operation using larger registers.

However the question is: are the floats logically an array or four independent values?

In other words will you ever need v[i] where i is a variable? If you do then using four members will be annoying because it will require a switch, if you never need it instead it will be annoying to use v[0] instead of x.

The floats belonging together logically. I was wondering especially about the handling with stl-containers. When i have something like that:<br\> std::vector<ClassA> vec (10000);<br\> generate (vec.begin(), vec.end(), [&]()->ClassA{<br\> ClassA tmp(0,0,0,0);<br\> return tmp;<br\> });<br\> is it better to use an array for the swap operator for instance?
–
HymirSep 9 '13 at 17:57

Please consider this caution from the C++ FAQ: Arrays Are Evil. The final paragraph:

To net this out, arrays really are evil. You may not think so if
you're new to C++. But after you write a big pile of code that uses
arrays (especially if you make your code leak-proof and
exception-safe), you'll learn — the hard way. Or you'll learn the easy
way by believing those who've already done things like that. The
choice is yours.

EDIT: After seeing the comment from @Eric, I investigated and found this insightful question: Should arrays be used in C++?. Some of the answers there indicate that declaring a member variable as a fixed-sized array can indeed be an appropriate use of an array in C++.

Generally speaking, though, as the C++ FAQ asserts, arrays should be used with caution in C++.

That diatribe is generally inapplicable to this situation.
–
Eric PostpischilSep 9 '13 at 23:53

I read that link. In short he was very biassed, trying to prove that in every case one should use container classes and nothing else. Thank God such people are not in the C++ review committee and only maintain their pathetic websites!
–
Simple FellowSep 13 '13 at 13:16