C# Real Time Try Catch

前端 未结 2 520
暖寄归人
暖寄归人 2021-01-05 05:57

I\'d like a response from someone who actually does real-time programming in C# or who really understands the language internals.

I know that exceptions should not b

相关标签:
2条回答
  • 2021-01-05 06:29

    Good discussion, with metrics, of performance implications of try catch here.

    Do try/catch blocks hurt performance when exceptions are not thrown?

    0 讨论(0)
  • 2021-01-05 06:46

    .NET exceptions have a very, very low overhead cost unless they are thrown. Having a try/catch block in place will have a very minimal performance impact. I have found nearly no impact, even in very fast, tight loops, to having exception handling in place.

    However, exceptions in .NET are VERY expensive when they're thrown. They tend to be much high-impact on performance if you throw them than many other languages. This is due to the full stack information gleaned when the exception is created, etc.

    This is the opposite behavior to some other languages, such as python, where exception handling has a higher cost, but throwing is actually fairly performant.

    However, if you are concerned, I would suggest you profile your routine, and test it yourself. This has been my experience after quite a bit of performance profiling. There is no substitution for measuring in your own codebase.

    0 讨论(0)
提交回复
热议问题