How do I efficiently set which code branch to use in a low-level C# function?

I’m writing a relatively straight-forward physics simulation, in which many small indicator points are initialized. The core of the code is in one project, with two different interface projects (i.e. two solutions) both using the same core.

The indicator points are instances of a class, Point, which is itself inherited by several other classes (to allow new properties and physics to be considered). Each Point has a location (x,y,z) and a The fundamental method in Point is Advance, which calculates the location update using the Runge-Kutta 4th-order (RK4) method. With many points this can become quite expensive, so for debugging and experimentation purposes I want to be able to switch to a lower-order method (e.g. simple first-order Euler integration, or perhaps a predictor-corrector method).

My question is: what is the most efficient way to handle this situation?

I’ve so far thought of three solutions:

  • Option 1: Declare a bool EulerUpdate at program initiation which is passed to the Point constructor (and of course to the constructor of all inheriting classes). This can then be used in one of two ways:
    • Option 1a: Set a private bool in the class, say UseEuler. Within Advance, check if UseEuler is true; if so, run the faster code. If false, use the slower, more accurate RK4 code.
    • Option 1b: Set a private function in the class, say UpdateCalc. Within the constructor, set UpdateCalc to either a method representing the Euler integration method (e.g. UpdateCalc = EulerUpdate) or the RK4 integration method (UpdateCalc = RK4Update) depending on the value of EulerUpdate. In Advance, just call UpdateCalc.
  • Option 2: Use preprocessor directives in Advance (i.e. #if EULERINTEGRATION) and then declare those in the csproj.

The problems I have with each are as follows:

  • Option 1: Both of these seem like they’ll be slow because they involve either a) putting an if statement in very low-level code or b) doing some runtime shenanigans (polymorphism? I guess?). However, I do not know anywhere near enough about performance (in particular, performance in C#) to be able to comment sensibly.
  • Option 2: While this seems guaranteed to be the most efficient option, it comes with the problem that it would mean I have to recompile the core library for each interface. I’m reluctant to take that hit if I don’t have to.

I’d be grateful to hear your perspectives on this; I’ve tried option 1 and found that it provided a serious benefit in run time, but I’m hopeful that I might both be able to do something better and to learn something more fundamental about writing efficient code at the same time. Thanks!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật