Выгода попытки реального времени C#

Да, абсолютно. Использование Отражения:

static IEnumerable<Type> GetTypesWithHelpAttribute(Assembly assembly) {
    foreach(Type type in assembly.GetTypes()) {
        if (type.GetCustomAttributes(typeof(HelpAttribute), true).Length > 0) {
            yield return type;
        }
    }
}
7
задан Mark T 28 August 2009 в 15:51
поделиться

2 ответа

.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.

11
ответ дан 6 December 2019 в 21:17
поделиться

Хорошее обсуждение, с метриками, последствий использования try catch для производительности.

Могут ли блоки try / catch ухудшать производительность, когда исключения не генерируются?

1
ответ дан 6 December 2019 в 21:17
поделиться
Другие вопросы по тегам:

Похожие вопросы: