简体   繁体   English

尝试捕捉性能 c#

[英]try catch for performance c#

My knowledge with try catch is limited.我对 try catch 的了解有限。 But i wonder if it can be used to gain performance.但我想知道它是否可以用来获得性能。

Example, i am creating this voxel engine where a function is like this:例如,我正在创建这个体素引擎,其中一个函数是这样的:

Block GetBlockInChunk(Vector Position){
    if(InBound(Position)){
        return Database.GetBlock();
    }
    return null;
}

Here it has to check bounds of the given position, with using try catch, then you can remove them?这里它必须检查给定位置的边界,使用try catch,然后你可以删除它们吗?

Block GetBlockInChunk(Vector Position){
    try{
        return Database.GetBlock();
    }
    catch{
        return null;
    }
}

I feel like this is probably terrible practice, but i am curious.我觉得这可能是可怕的做法,但我很好奇。

The link I provided in the above comment shows a description of why you shouldn't ever use a try-catch when an if-statement would prevent the exception from being thrown, but in the interest of showing performance in terms of actual numbers, I wrote this quick little test program.我在上述评论中提供的链接显示了为什么在 if 语句会阻止抛出异常时不应该使用 try-catch 的描述,但为了根据实际数字显示性能,我编写了这个快速的小测试程序。

Stopwatch watch = new Stopwatch();

int[] testArray = new int[] { 1, 2, 3, 4, 5 };
int? test = null;

watch.Start();
for (int i = 0; i < 10000; i++)
{
    try
    {
        testArray[(int)test] = 0;
    }
    catch { }
}
watch.Stop();

Console.WriteLine("try-catch result:");
Console.WriteLine(watch.Elapsed);
Console.WriteLine();

watch.Restart();
for (int i = 0; i < 10000; i++)
{
    if (test != null)
        testArray[(int)test] = 0;
}
watch.Stop();

Console.WriteLine("if-statement result:");
Console.WriteLine(watch.Elapsed);

The result of the program is this:程序的结果是这样的:

try-catch result:
00:00:32.6764911

if-statement result:
00:00:00.0001047

As you can see, the try-catch approach introduces significant overhead when an exception gets caught, taking over 30 seconds to complete 10,000 cycles on my machine.如您所见,try-catch 方法在捕获异常时引入了大量开销,在我的机器上完成 10,000 个周期需要 30 多秒。 The if-statement, on the other hand, runs so fast that it is basically instantaneous.另一方面,if 语句运行得如此之快,以至于它基本上是瞬时的。 Compared to the try-catch, this is a performance improvement in the neighborhood of 3,000,000%.与 try-catch 相比,这是大约 3,000,000% 的性能改进。

(This isn't a rigorous benchmark, and there are ways to write it and run it differently to get more precise numbers, but this should give you a good idea of just how much more efficient it is to use an if-statement over a try-catch whenever possible.) (这不是一个严格的基准测试,有很多方法可以编写它并以不同的方式运行它以获得更精确的数字,但这应该会让您很好地了解在尽可能尝试捕获。)

It is about 2 years later, but it is still relevant...大约两年后,但它仍然相关...

I respect the other replies but I believe there is a basic misunderstanding here for the purpose of try-catch.我尊重其他回复,但我相信这里存在一个基本的误解,用于尝试捕获。 Try Catch is a very effective way within the C# and dotnet, to identify errors over the development and the life of the code. Try Catch 是 C# 和 dotnet 中的一种非常有效的方法,可以在代码的开发和生命周期中识别错误。 It is never intended to be a tool to use different, and if it is been fire, it means that you have a bug that needs to be fixed.它从不打算成为使用不同的工具,如果它着火了,则意味着您有一个需要修复的错误。

The problem it comes to solve is the standard error message that stops the code and then you need to dig in. With try and catch you can know in which method the problem occurs and narrow down the search.它要解决的问题是停止代码的标准错误消息,然后您需要深入研究。通过 try 和 catch,您可以知道问题发生在哪种方法中并缩小搜索范围。

As standard, I wrap ALL my methods with try-catch and added an additional functionality that writes the error message with the name of the method and additional essential information like time, and some helpful anchors of data to a debug file, which I can access even when the code is in production.作为标准,我使用 try-catch 包装了我的所有方法,并添加了一个附加功能,该功能将带有方法名称和附加基本信息(例如时间)的错误消息以及一些有用的数据锚点写入调试文件,我可以访问该文件即使代码在生产中。 This is priceless!这是无价的!

As far as performance, if the try-catch doesn't fire (which should be normal), there is no performance reduction, as it is merely a simple warper.就性能而言,如果 try-catch 没有触发(这应该是正常的),则不会降低性能,因为它只是一个简单的扭曲器。 If someone is really into a high level of performance and every fraction matter, it is possible to eliminate it using precompiler conditions (#if...).如果有人真的对性能非常感兴趣并且每个部分都很重要,则可以使用预编译器条件 (#if...) 来消除它。

Hope this is helpful.希望这是有帮助的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM