python异常处理比PHP和/或其他语言更有效吗? [英] Is python exception handling more efficient than PHP and/or other languages?

查看:126
本文介绍了python异常处理比PHP和/或其他语言更有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经钻进了我的头(至少在PHP中)它是badbadmojo使用 try ... catch 块进行流量控制。我所学到的是仅使用它们来处理意外错误,而不是确定程序的逻辑流程,因为 catch 块是昂贵的。



现在我正在学习python,我看到很多例外,而 EAFP 原则。这是否意味着python在处理异常方面更有效率,所以我不需要担心流量控制,还是原则仍然存在?如果没有,PHP是从规范(与其他语言相比)的例外,还是Python?

解决方案

从历史上看,像C ++这样的语言,与其他形式的流控制相同的语言相比,异常非常慢。



在C ++中,有两件事在工作中:




  • 抛出异常非常复杂。堆栈需要解开,而在本地代码中这样做比在基于VM的高级语言中更难。

  • 定期的直接流控制是非常快的它是本地代码;一个分支是几个指令,其中一个异常回滚堆栈调用一个复杂的算法(查找一个大的,可能压缩的表中的堆栈数据等等)。



这种性能差异导致了异常背后的一般智慧:只对异常事物做了一些这样的事情,所以它只用于最有利的地方,而不是会影响性能的地方。



这不适用于高级语言。这也有两个原因:




  • 回滚堆栈要简单得多。堆栈很容易检查;您不需要魔术表来了解在任何给定时间内回滚堆栈以及构建什么对象的距离。

  • 常规程序流本质上较慢。在基于虚拟机的语言中,一切都需要更多的工作才能开始。



异常仍然不是免费的,但差异是不再需要担心了。这意味着C ++中形成的一般智慧在这里被误用了。例外情况在正常程序流程中经常使用。



事实上,它们内置于语言中,在您始终使用的结构中。每次使用迭代器 - xrange(1000)中的x的每个 ,将使用 StopIteration 异常结束循环。



在Python中选择异常或线性流控制是更有意义的。不要根据性能选择,除非你实际上处于性能重要的内在循环中;在这种情况下,一如既往地剖析,并找出是否真的重要。



(我不能说PHP。)


I have it drilled into my head that (at least in PHP) it is badbadmojo to use try... catch blocks for flow control. What I've learned is to use them only to handle unexpected errors, not determine the logic flow of the program, because catch blocks are expensive.

Now that I'm learning python, I see a lot of exceptions everywhere and the EAFP principle. Does this mean that python is more efficient at handling exceptions, so I don't need to be as worried about them for flow control, or does the principle still stand? If not, is PHP the exception from the norm (as compared to other languages), or is Python?

解决方案

Historically, in languages like C++, exceptions have been very slow compared to other forms of flow control in the same language.

In C++, there are two things at work:

  • Throwing an exception is very complex. The stack needs to be unwound, and doing so in native code is much harder than in a high-level VM-based language.
  • Regular, direct flow control is extremely fast. It's native code; a branch is a couple instructions, where an exception rolling back the stack invokes a complex algorithm (looking up stack data in a large, possibly compressed table, and so on).

This disparity in performance led to the general wisdom behind exceptions: only do it for unusual things, so it's only used where it's most beneficial and not where it'll hurt performance.

This does not apply to high-level languages. This is also for two reasons:

  • Rolling back the stack is much, much simpler. The stack is very easy to examine; you don't need magic tables to know how far to roll back the stack and what objects are constructed at any given time.
  • Regular program flow is inherently slower. In a VM-based language, everything simply takes more work to begin with.

Exceptions still aren't free, but the disparity is no longer something to worry so much about. This means the general wisdom formed in C++ is misapplied here. Exceptions are regularly used in normal program flow.

In fact, they're built into language, in constructs you use all the time. Every time you use an iterator--every for x in xrange(1000), a StopIteration exception is used to end the loop.

Choose exceptions or linear flow control in Python based on which makes more sense. Don't choose based on performance, unless you're actually in an inner loop where performance matters; in that case, as always, profile and find out if it actually matters.

(I can't speak for PHP.)

这篇关于python异常处理比PHP和/或其他语言更有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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