This applies .NET. I am looking to write a spatial mapping application. There will be several polygons in memory at once (on the order of 30 - 50 polygon). each polgon has a collection of LatLong points. the collection can range from 10 to 200 per polygon. However, there are alot of calculations that will be done using the points, which is why (for performance) I want to make the LatLong a struct. However I am weary do to the large number of LatLong that will be in memory. Any insight to this will be appreciated. To recap: I want to know if I shold make the LatLong a struct because I want performance ont the calculation, or a class because of the number of latLongs that will be in memory at one.
-
Check out: http://stackoverflow.com/questions/521298/when-to-use-struct-in-c You may answer your own question there. – Marc Mar 30 '11 at 14:21
-
2As usual, first and foremost you should make it a struct *if you want value semantics*. After making that decision (do you want value or reference semantics?), measure. Then ask yourself: "Is this performance good enough?" – R. Martinho Fernandes Mar 30 '11 at 14:23
-
This is an absolute FAQ. Learn to search or try narrowing the question down to the specific detail of the question that you want additional guidance with. (PS. writing clear English will help a lot as well) – sehe Mar 30 '11 at 14:41
4 Answers
The answer to this is going to depend on your resource priorities, what's in the class/struct, and how you're using it. I suggest you figure out what your memory/performance resources are like, then do a lot of testing of both implementations to see how they fit in those resource parameters. If possible, try to test the actual operations you expect to perform.
MSDN also offers some good guidance on when and when not to use structs. From the article:
Do not define a structure unless the type has all of the following characteristics:
- It logically represents a single value, similar to primitive types (integer, double, and so on).
- It has an instance size smaller than 16 bytes.
- It is immutable.
- It will not have to be boxed frequently.

- 30,035
- 12
- 80
- 104
Whether you should do one or the other depends entirely on your application. I would try both ways and see which one is faster. That's what I do when I have that question.

- 84,912
- 12
- 139
- 238
In general, if you're going to have a large collection of simple objects where the data contained in them won't change once instantiated (your latLongs sound like they're basically data containers) then I would personally use an immutable struct.

- 7,512
- 3
- 33
- 43
Make two test implementations with a representative calculation, one with a struct and one with a class, and measure, I repeat measure (!!!) the performance. To my experience it very likely that any prejudice about the expected performance turns out beeing totally wrong in such kind of situations.

- 19,739
- 7
- 52
- 88
-
Agree, but it will be a lot better to use the actual real calculations. You need to measure the performance of your system, not that of some system that may or may not have the same performance characteristics. It's not always easy to decide what is a representative calculation. – R. Martinho Fernandes Mar 30 '11 at 14:30
-
I agree, using the real system would bring the best results. On the other hand, when the real systems will have >50.000 lines of code, a wrong design decision will not be fixed as easily as at the time when the system had only 500 lines of code. – Doc Brown Mar 30 '11 at 14:38