Struct vs class

This applies to .NET. I want to write an application for spatial mapping. In memory there will be several polygons at once (about 30-50 polygons). Each halfgon has a set of LatLong points. collection can vary from 10 to 200 per landfill. However, there are many calculations that will be performed using points, so (for performance) I want to create a LatLong structure. However, I am tired of the large number of LatLongs that will be remembered. Any understanding of this will be appreciated. Recall: I want to know if I can make a Shadow for the LatLong structure because I want the performance to be calculated, or the class due to the number of latLongs that will be in memory on one.

+3
source share
5 answers

The answer to this will depend on your resource priorities, what is in the class / structure, and how you use it. I suggest you find out what your memory / performance resources are, and then do a lot of testing on both implementations to see how they fit these resource parameters. If possible, try checking the actual operations that you expect to complete.

MSDN also offers some good recommendations on when and when not to use structures. From the article:

Do not define a structure if this type does not have all of the following characteristics:

  • It logically represents a single value similar to primitive types (integer, double, etc.).
  • It has an instance size less than 16 bytes.
  • It is unchanging.
  • It does not need to be inserted into the box often.
+10
source

Whether you should do this or that is entirely up to your application. I would try in both directions and see which one is faster. This is what I do when I have this question.

+1
source

In general, if you have a large collection of simple objects in which the data contained in them will not change after creating the instance (your latlongs sound like they are data containers), then I personally would use an immutable structure.

+1
source

Make two test implementations with representative calculation, one with structure and one with class, and measure, I repeat measure (!!!) performance. In my experience, it is very likely that any prejudice regarding expected performance will be completely wrong in such situations.

0
source

Source: https://habr.com/ru/post/1396876/


All Articles