红宝石接球和效率 [英] ruby catch-throw and efficiency

查看:47
本文介绍了红宝石接球和效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

catch 旨在跳出深度嵌套的代码.在 Java 中,例如可以使用 Java 的 try-catch 来处理异常,但它被认为是糟糕的解决方案,而且效率也很低.在用于处理异常的 Ruby 中,我们有 begin-raise-rescue,我认为将其用于其他任务也很昂贵.

catch in Ruby is meant to jump out of deeply nested code. In Java e.g. it is possible to achieve the same with Java's try-catch meant for handling exceptions, it is however considered poor solution and is also very inefficient. In Ruby for handling exceptions we have begin-raise-rescue and I assume it is also to expensive to use it for other tasks.

Ruby 的 catch-throw 真的是比 begin-raise-rescue 更有效的解决方案,还是有其他原因可以用它来破坏嵌套块而不是 开始救援?

Is Ruby's catch-throw really a more efficient solution then begin-raise-rescue or are there any other reasons to use it to break nested blocks instead of begin-raise-rescue?

推荐答案

除了是摆脱控制结构的正确"方式之外,catch-throw 也明显更快(10 倍)在我的测试中一样快).查看 this gist 了解我的代码和结果.

In addition to being the "correct" way to get out of control structures, catch-throw is also significantly faster(10 times as fast in my testing). Check out this gist for my code and results.

这篇关于红宝石接球和效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆