Hi, Having read lots of considerations, some experiments seemed overdue. So I developed a little test to actually see memory footprint and execution times for the different complex number approaches. My test first allocates and initializes 5 million complex numbers stored - as two float arrays - as one array of class ComplexClass instances - as one array of struct ComplexStruct as expected the numbers were 40MB, 100MB and 40MB respectively, proving the class instance takes 8B of overhead plus 4B for the pointer (32bit Windows), the struct has no memory overhead. Then I continued to measure the time required to square these numbers; I did 5M multiplies, involving only the first 16K of the numbers in each array (in order to avoid cache misses), and measured elapsed time. A release build with Visual 7.1 (.NET 1.1) and Visual 8.0 (.NET 2.0) respectively, running on a Pentium M at 1.7GHz, performing multiple passes, yielded the following numbers:
.NET 1.1 .NET 2.0
floats 4110 msec 3970 msec
class 4480 msec 4330 msec
structs 4150 msec 4080 msec
ref structs 4060 msec 3970 msec
The good news here is performance is very much the same in all cases (slightly over 1M mul/sec); the class is some 10% slower, and the ref struct is the best; also NET2.0 is marginally faster than NET1.1 here. Conclusions: - struct is cheaper than class in RAM and cycles (as expected) - ref struct is faster than struct (as expected) but only marginally so - the hard way does not bring anything with respect to ref structs (the positive surprise). So whenever I would need complex numbers, I would go for structs. The compiler seems good enough to make the hard way pointless. Cheers
Luc Pattyn