When are structs the answer?

前端 未结 12 797
广开言路
广开言路 2020-12-12 18:24

I\'m doing a raytracer hobby project, and originally I was using structs for my Vector and Ray objects, and I thought a raytracer was the perfect situation to use them: you

12条回答
  •  有刺的猬
    2020-12-12 18:44

    The first thing I would look for is to make sure that you have explicitly implemented Equals and GetHashCode. Failing to do this means that the runtime implementation of each of these does some very expensive operations to compare two struct instances (internally it uses reflection to determine each of the private fields and then checkes them for equality, this causes a significant amount of allocation).

    Generally though, the best thing you can do is to run your code under a profiler and see where the slow parts are. It can be an eye-opening experience.

提交回复
热议问题