THE EXECUTION MODEL





THE EXECUTION MODEL

Although not a vital part of your understanding of the differences between Perl and Python, understanding the similarities and differences between the two models of execution is a useful side note.

Perl

Perl's execution model, follows a very simple path. The model assumes you've just run a Perl script from a text file, which with some minor differences applies equally to the methods used for executing embedded scripts (from C/C++), in-line evaluations (through eval), and the one line scripts supplied on the command line.

Following the figure the basic execution process for Perl goes like this:

  1. Read the source code.

  2. Immediately execute any BEGIN blocks in the source.

  3. Import (and if necessary evaluate and execute) the text from an external module (imported using use or require) so that it becomes part of the text of the script. Execution only occurs for modules that define BEGIN blocks. Modules which export symbols into the caller's namespace create the necessary aliases and symbol table entries at this point.

  4. Once all the code required to execute the script has been imported, compile the script into the internal opcode format used by the Perl "virtual machine."

  5. Optimize the opcode tree generated in stage 5, reducing statements using a combination of lookahead and constant folding.

  6. Follow the opcode tree generated in stage 5 to actually execute the code.

Unfortunately, this execution model implies an overhead during the execution process because the "import," compilation, and execution sequence occurs each time you execute a Perl script. There are of course solutions to this in specific environments – for example the mod_perl and PerlEx web server extensions which improve the performance of CGI scripts – but there is no standard method for all scripts.

Python

As already stated, Python is an object-oriented language that relies not on the manipulation of a number of internal data types using built-in functions, but works using a series of external classes and methods in combination with other classes, objects, and methods that you define within your Python script.

Because Python modules are treated as classes, and because of the strict naming system employed to support it, Python can import compiled versions of the modules and classes, rather than import the text used to describe them. This makes the Python execution process:

  1. Read the source code.

  2. Once all the code required to execute the script has been imported, compile the source script into the machine code format used by the Python virtual machine.

  3. Optimize the machine code generated in stage 5, reducing statements using a combination of lookahead and constant folding.

  4. Follow the opcode tree generated in stage 5 to actually execute the code.

  5. Any import statements or class inheritance requirements are handled during the execution process – only those import statements which are actually required to be executed are ever honored.

The critical difference is stage 5, which is roughly equivalent to stage 3 in the Perl execution sequence. Python imports modules only when they are requested to be imported. Furthermore, by the time the import statements are executed the source script has already been compiled into bytecode. In order to avoid importing and recompiling the entire script Python imports pre-compiled versions of the modules requested. If the modules are not already compiled then they are compiled and imported. The compiled version is saved, so that next time the module is requested the compiled version can be imported directly.

By using pre-compiled modules we eliminate the slowdown experienced by a complex Perl script. In Perl, if we import 10 or 20 modules their contents will be re-parsed and compiled every time the script is called, even though it's unlikely that the module source code has changed since the last time it was executed. Although this seems insignificant, it can increase the execution time considerably, especially on scripts that may be run many times – for example on a web server. The mod_perl extension for Apache, or ActiveState's PerlEx extension for Microsoft's IIS web server get round this by retaining the byte compiled versions of an entire script in memory. These are, however, web-only solutions.

Perl does come with a byte compiler (it's part of the Perl compiler system), but it's not used and the system is not enabled or enforced by the interpreter.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows