Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is python exception handling more efficient than PHP and/or other languages?

Tags:

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?

like image 281
keithjgrant Avatar asked Jan 17 '11 20:01

keithjgrant


People also ask

Is Python better than PHP?

Python is powerful, portable, open source and is relatively easy to learn and fun to use. It has many features that other programming languages do not support. It's syntax is simpler and code is more readable in Python compared to other programming languages like PHP, C and C++.

Why is Python more secure than PHP?

Python is more secure than PHP. It has many security features that you can leverage to build complex applications with high-end functionality and clear goals. In fact, this March 2019 report shows that Python is one of the most secure programming languages.

What are the advantages of exception handling in Python?

Exception handling allows you to separate error-handling code from normal code. An exception is a Python object which represents an error. As with code comments, exceptions helps you to remind yourself of what the program expects. It clarifies the code and enhances readability.

What is faster Python or PHP?

Speed is a critical factor to judge the performance of two frameworks. Each time a file is created or modified; Python converts the code into bytecode. This code compilation method makes Python quicker than PHP. PHP programmers can simply improve the speed of PHP applications by installing a variety of caching systems.


2 Answers

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

like image 186
Glenn Maynard Avatar answered Oct 11 '22 10:10

Glenn Maynard


I don't believe that the EAFP encourages the use of exceptions for flow control. Rather, it tells us that we needn't bother checking for the existence of a particular key in a dictionary or property of an object before we reference it.

Throwing exceptions as an alternative to if statements or having correct while statements or instead of using break or continue inside of a loop does not fall under that category. That's lazy, error-prone progamming and it should be avoided.

like image 24
Adam Crossland Avatar answered Oct 11 '22 09:10

Adam Crossland