A selection of online compilers: we run and test the code directly in the browser. Complete memory statistics

PHP is an interpreted programming language; with each request, the source code is analyzed and “executed”. This approach, of course, is very convenient at the project development stage, but it introduces an extra step into the process of executing production code. Thus the interpretation, at first glance strong point PHP costs extra CPU time and resources.

Below we will talk about compilers that allow you to compile php code in C++, and its in the executable. Thus, PHP applications are executed directly by the processor, bypassing the interpreter.

Let's check if everything is so good in practice.

How the interpreter works

Interpretation of PHP code takes place in two stages:

  1. Code parsing and generation of opcodes (Zend opcodes) - instructions understandable to the interpreter.
  2. Execution of opcodes.

While the first phase lends itself well to optimization (using an opcode cache), the second is quite closed - the interpreter is always an intermediary between the set of instructions and the processor executing them. Without an interpreter, the processor cannot understand what to do with opcodes.

To get rid of the interpreter link, compilers were invented, the most popular and recent of which is HipHop from Facebook. Let's feel it closer.

HipHop PHP

HipHop is written by Facebook developers and is an application that:
  1. optimizes PHP code
  2. converts to C++
  3. generates from your application a multi-threaded web server that executes it
  4. compiles into executable code using g++

Thus, the input is PHP code, the output is a server, part of which is the written functionality.

Let's check how HipHop can handle compiling an application written using a framework, such as Wordpress.

Compiling Wordpress

After installing HipHop, in the src/hphp/ folder we will get the hphp file, which is the compiler. Before compilation starts, set the environment variables:

Cd .. # go to the folder with hiphop export HPHP_HOME=`pwd` export HPHP_LIB=`pwd`/bin export CMAKE_PREFIX_PATH=`/bin/pwd`/../

and go ahead!

Download Wordpress and unzip the archive:

Wget http://wordpress.org/latest.tar.gz tar zxvf latest.tar.gz

Copy wp-config-sample.php to wp-config.php and specify the settings for connecting to the database (in the host settings we specify 127.0.0.1, not localhost).

For successful compilation you need to patch Wordpress a little:

  1. Open wp-includes/js/tinymce/plugins/spellchecker/classes/SpellChecker.php and replace: function &loopback(/* args.. */) ( return func_get_args(); ) with function &loopback(/* args.. */ ) ( $ret = func_get_args(); return $ret; )
  2. In wp-includes/query.php, instead of if (!isset($q["suppress_filters"])) $q["suppress_filters"] = false; insert $q["suppress_filters"] = true;

Wordpress is ready.

Hiphop needs to specify a list of files that we will compile - we will get it and save it in files.list:

Find. -name "*.php" > files.list

Everything is ready for compilation, let's proceed:

$HPHP_HOME/src/hphp/hphp --input-list=files.list -k 1 --log=3 --force=1 --cluster-count=50

After completing the command, in the temporary folder (at the beginning of compilation, hphp will show its path, something like “/tmp/hphp_ptRgV1”) we will receive a compiled web server. Let's launch it (if something is hanging on port 80, for example apache or nginx, you must first stop it to free the port):

Sudo /tmp/hphp_6s0pzd/program -m server -v "Server.SourceRoot=`pwd`" -v "Server.DefaultDocument=index.php" -c $HPHP_HOME/bin/mime.hdf

Voila! By going to http://localost we will see a working Wordpress blog.

Performance

Let's see if there will be a performance increase compared to the uncompiled version of WordPress running on apache2. Below are graphs of the dependence of page generation speed on the number of parallel users.

As you can see, the results were shocking: the compiled blog runs on average 6 times faster! The average number of requests processed per second in the uncompiled version is 9, and in the compiled version it is 50! I don’t know about you, but these results amazed me; I didn’t expect such a strong performance increase.

Summarize

After such stunning results, only one thing can be said - the guys from Facebook did a great job. The compiler really makes a rocket out of an application, and although the application needs to be prepared before compiling, the result is worth it.

To the point:

If you liked the post, click on Google +1 - it will give me more motivation to write and it will just be a pleasure.

All the free PHP compilers that are presented here can rebuild PHP scripts into machine code that can run on a computer without downloading a special PHP interpreter, or compile them into a bytecode command line interface (NET or Mono framework or Java bytecode is required for installation (where a Java Virtual Machine is required for installation)).

Such compilers can be useful for a variety of purposes: they can speed up the execution of your script because they are no longer interpreted at runtime; or thanks to them, you can distribute your applications without revealing the source code (which other commercial scripts require). I suppose they are also suitable for the case where someone wants to write web-dependent PHP programs and distribute them with the functionality of running on the desktop (as opposed to regular web applications that run on a server), this is all possible because that PHP is an easy-to-learn programming language and basically contains many built-in functions with Internet access. (In this case, you will either have to distribute applications with a built-in web server or use a compiler that compiles the server into your application.)

By the way, if you want to use obfuscation in your code, then know that this is also possible when using PHP accelerator. Such accelerators also imply an increase in the speed of execution of your script.

Useful information for those who do not yet know that the official version of the PHP translator can be downloaded from the PHP website: Hypertext Processor.

Free PHP compilers for composing native code, .NET or Java bytecode scripts.

Bambalam (new)

This program produces independent Windows applications EXE for your PHP source code. It's not really a compiler for native code, since it just encodes source and embeds a PHP interpreter, but this program is certainly suitable for people looking for native and byte-code compilers. By the time the entire program was written, its execution environment was built-in PHP version 4.4.4 (the program has not been updated since 2006). The source code for Bambalam is publicly available.

Phalanger (for .NET)

Phalanger compiles your PHP code into CLI bytecode (.exe or .dll). This program can be run through .NET 4.0 or Mono frameworks. Your PHP code can use any .NET objects and additional standard libraries PHP extensions. The resulting NET assembly can be either signed or hidden. This program also produces templates that allow you to create PHP applications using Visual Studio. The program is released under the Apache license.

HipHop for PHP (for native code)

HipHop translates your PHP code into C++ code, which is later compiled using the GNU C++ compiler into executable binary code. The compiler supports all the features of PHP 5.3 (except of course for things like eval()). It works and compiles code for 64 bit Linux versions and FreeBSD. Since the program is distributed in source code form, you will have to compile manually (yourself). It is released under the PHP License.

Roadsend PHP (for native code).

The Roadsend PHP compiler produces native binaries (executables) for Windows and Linux. Your scripts are not limited to console programs (command lines): they can be built using embedded web servers, allowing them to run in the same way they run on a website, albeit on your own user system. The compiler is released under the GNU GPL license and runs under the GNU LGPL. Unfortunately, the program has stopped its active development.

Project Zero (for Java)

(Note: this appears to be software is now non-existent. The site has been inaccessible for half a year now.) Project Zero includes a compiler and CLR that can compile your PHP code into Java bytecode and run it. Note that Project Zero is more than just a PHP compiler/runtime; it provides a rich framework that allows you to enhance web applications using PHP or Groovy (another scripting language). This compiler is available for Windows, Mac OS X and Linux. In order to work with this compiler, you will need to download the Java Development Kit.

Which of the proposed compilers do you prefer? Or do you have another favorite translator? Leave your remarks and comments below, we will be happy to read them and reel them in.

Tags: PHP compilers, script translation

Compiling PHP from source code is often done on Unix-like systems. Those who work in a Windows OS environment will most likely download and install PHP from binary packages. And while I don't agree that it's easier to use a precompiled solution, even on Unix systems there are some benefits that can come with compiling a binary from source. All in all:

  • You have a chance fine tuning final product when compiled. Maybe you want a certain extension that compiles directly to the binary rather than loading it as an external library. Or perhaps you want to turn off something that is usually available by default.
  • You can do a trick, during the compilation process if necessary, that may improve performance for a specific environment (of course, this assumes you already know what you're doing in this case you wouldn't read this article !).
  • Compiling may be the only way to get things to work if compiled binary files were built on older versions with support for software and libraries, and now you are working on a new system.

Warning: compilation can be frustrating, especially on Windows! You must ensure the build environment is set up correctly, learn how to use the compiler and other build tools properly, and satisfy any library dependencies. We hope this article is your first step in overcoming many of these obstacles.

Setting up the build environment

PHP is written in C and therefore a C compiler is required if you are going to build PHP from source code. C++ is a super suite of C, so a good C++ compiler should be able to compile C code, and although this is not always the case. For Windows Visual Microsoft, C + + Express (which will later be called VC + +) is freely available on the Microsoft website. The 2010 edition was used.

When choosing a compiler version, you should keep in mind how you will run PHP. If you have to work with mod_php of the officially compiled Apache binaries, and you want to, compile PHP using Visual Studio 6, since this is the Apache compilation version. The module must target the same runtime library as Apache, in this case msvcrt.dll. If you are building Apache from source as well, or if you are going to run PHP as FastCGI or CLI, then this is not a problem and 2010 will work fine.

You must also install software Windows software Development Kit (SDK after). The SDK gives us important header files for Windows platforms, which we need for successful compilation. This too, version 7.1 was used.

Install the compiler and then the SDK. I won't discuss installation as both have a graphical installation wizard that will guide you through the entire process.

If you have a working compiler build, download the Binary Tools and Known Packages from windows.php.net. The binary tools package (I'm using the 20110915 archive) contains development tools like re2c, bison, and some additional commands that you will need to build PHP. The known package (I'm using the 5.4 archive since it matches the version of PHP I'll be compiling) contains the minimal headers and dependency libraries needed, eg zlib.h.

It probably goes without saying that you want to download the PHP source as well from windows.php.net. As of this writing, the current version of PHP is 5.4.6, so that's the version number you'll see in the examples.

This good idea, to create working space, to which you can extract the source code and compile it without affecting the rest of your system. Create a folder C:\PHP-Dev that will serve as your working directory, and then extract the binary archive and tools into it.

Next, extract the contents of the archive, PHP source in C:\PHP-Dev now you have php5.4 in the source folder and then extract its deps archive into the deps sibling folder. The directory structure should look something like this:

Open the Windows SDK Command Prompt that was installed with the SDK (Start => Microsoft Windows SDK => Windows SDK Command Prompt) and run the following commands:

Setenv /release /xp /x86 cd C:\PHP-Dev bin\phpsdk_setvars.bat

Using the SDK console command line is preferable before the regular cmd.exe console, as it sets many environment variables specific to source code compilation. Compilations of commands later must also be done in this console.

setenv sets some build properties for the environment, in this case the target environment is Windows XP 32-bit version of the build. You can try and build with /x64 if you're looking for adventure. Definition of different Windows versions, such as /Vista, will most likely have exit problems due to some strange definitions in the scripts (PHP still wants to be XP compatible). Unless you really know what you're doing, it's probably safest to stick with the recommended values ​​I used above.

The phpsdk_setvars.bat script accesses some additional environment variables, the build process was able to find the binary tools.

Keep in mind that all these variable settings are only temporary sessions in the console. If you quickly close everything to come back to compile later, you will need to run the command again, and if you don't, you will get errors like the following, when you later run configure, you will not be able to continue:

Checking for bison.exe ... ERROR: bison is required

Making sure you have the right build environment, the necessary sources, and any dependencies is the hardest part of the process. So now your environment is set up with source code and dependencies in place, it's time to compile!

Compiling PHP

IN command line SDK, go to your PHP source folder and run buildconf. The command is responsible for generating configuration files that will be created by the Makefile to control the compilation process.

After buildconf completes (this will only take a second), run configure --help - and examine the help for what features you want to enable/disable, then run configure again with whatever option you want. It's a good idea to check the output before moving, as it will warn you if any of the required dependencies are not available. If this happens, you can either install the dependencies and re-run the setup again, or adjust the calls to disable extensions that need them.

Finally, run NMAKE to start compiling.

Cd C:\PHP-Dev\php5.4 buildconf configure nmake nmake test

If any configuration or NMAKE fails, the problem is one of two: First, the environment is not configured correctly, second, you have enabled a feature that depends on external libraries and the libraries are not installed on your system. Double check that you have created the environment according to the instructions above, and that any additional libraries that may be required in the underlying configuration settings have been configured.

When the first NMAKE compilation process is complete you will find your new PHP files in the Release_TS folder. The NMAKE test runs new double capacity error tests to ensure everything is working as it should. NMAKE test results are sent to the QA team who depend on them to improve PHP, so it may take a few minutes to work on, it's a big deal.

At this point, you can also use the additional step of maintaining a NMAKE rig, which will create ZIP archives and binaries that can be copied around.

Compilation extensions

There are two ways to compile PHP extensions: statically and dynamically. A statically compiled extension is compiled into a PHP binary, while a dynamically compiled extension is one separate DLL that can be loaded later via a php.ini file. Extensions are typically compiled as DLLs, although there are some advantages to static compilation, and ultimately it depends on your needs.

To compile PHP extensions on Windows, extract the source code extension folder into the ext folder of your PHP source directory. Then, reconfigure the script by running buildconf --force and recompiling PHP using the appropriate options to enable the extension.

As an example, let's compile the AOP extension statically. Download the source code from PECL, and extract it into a folder, ext. Then follow these steps:

Cd C:\PHP-Dev\php5.4 buildconf --force configure --enable-aop nmake

The --force, buildconf option forces it to restore the configuration script. Next, run configure --help and you should see the option to enable the new extension in the output. In this case, it is --enable-AOP.

When nmake finishes, you will have a newly built PHP binary with AOP.

The extensions will be available as a DLL rather than baked in PHP, you can follow the same steps as above but specifying "shared" as the value for the configuration allows the option.

Buildconf --force configure --enable-aop=shared

As a result, the DLL will be in the Release_TS folder along with the PHP binary when compilation ends, in this case the name is php_aop.dll .

P.S.

Compiling on Windows is still a bit tricky, especially when it comes to extensions. Being able to compile source code is a good skill, especially if you want to change PHP later.

The article was prepared for you by the site team
Original article:
Translated by: Victor Klim

There are two types of programming languages: interpreted and compiled. What language is PHP? In order to answer this question, we need to understand the terminology.

A program that translates code written in one programming language into another is called a translator. A compiler is also a translator. It translates code written in the language high level, into machine code. The compilation process produces a binary executable file, which can already be run without a compiler.

An interpreter is a completely different category. The interpreter does not translate the code, but executes it. The interpreter analyzes the program code and executes each line of it. Every time you execute such code, you must use an interpreter.

In terms of performance, interpreters are significantly inferior to compilers, since binary code executes much faster. But interpreters allow you to fully control the program during its execution.

As for PHP, it is neither a compiler nor an interpreter. PHP is a cross between a compiler and an interpreter. Let's try to understand this and look at how PHP processes code.

Let's look at the picture:

We see that PHP is composed of two almost independent blocks - a translator and an interpreter. Why did you need to do this? Of course, for reasons of speed.

The PHP input is a script. It translates it (translates it), checking the syntax, into a special bytecode (internal representation). PHP then executes the bytecode (not the program code itself), but it does not create an executable file.

Bytecode is much more compact than ordinary program code, so it is easier (and faster) to interpret (execute). Judge for yourself: parsing is carried out only once at the translation stage, and a “semi-finished product” is executed - bytecode, which is much more convenient for these purposes. Therefore, PHP is more of an interpreter than a compiler. This “double work” was necessary for the following purposes.

Consider the loop:

For (i=0;i<10; i++) { Operator_1; Operator_2; Operator_3; ............ Operator_99; Operator_100; }

This cycle will “spin” 10 times. For each of these ten passes, the interpreter must 100 lines of code. And it needs to analyze and execute 10*100 = 1000 lines of code! If you convert the entire loop into bytecode once, then it will have to analyze 10 times less! This means that scripts will run 10 times faster!

It turns out that PHP is.

The main phase of PHP's work is the interpretation of the internal representation of the program and its execution. It is this phase that takes the most time in serious scenarios. However, the slowdown is not that significant.

It is worth remembering that PHP version 3 was a “pure” interpreter, and with PHP 4 scripts began to run much faster, since PHP version 4 (and PHP5) is an interpretive translator.

The Perl language, which is almost always called a compiler, works in exactly the same way - it translates program text into an internal representation, and then uses the resulting code during execution. So, we can say that PHP version 4 is a compiler in exactly the same way that Perl is.

So, we are forced to conclude that PHP is an interpreter with a built-in translation block that optimizes the flow of interpretation.

Using an interpreter (and therefore PHP) has its undeniable advantages:

  • There is no need to worry about freeing the allocated memory, there is no need to close files when you are finished working with them - the interpreter will do all the routine work, since the program is executed under its watchful control;
  • There is no need to think about variable types, and there is no need to declare a variable before its first use;
  • Debugging programs and detecting errors is greatly simplified - the interpreter has complete control over this process;
  • In the context of web applications, the interpreter also has a very important advantage - there is no danger of the server “freezing” if the program does not work correctly.

There are other advantages too. In general, using an interpreter can give scripts the power that Web users expect from them.

The performance penalty of PHP is noticeable in the case of large and complex loops, when processing a large number of lines, etc. However, note that this is the only drawback of PHP, which will appear less and less as more powerful processors are released, so that eventually , disappear altogether.

<<< Назад
(What's new in PHP5?)
Content Forward >>>
(Transition to PHP 5.3)

If you have any other questions or something is not clear - welcome to our

Almost all developers sooner or later face the need to run or quickly check some code, but not everyone knows that for such a simple task it is not at all necessary to run heavy desktop IDEs or application compilers. It is enough to use online tools that allow you to do everything much faster: Ctrl+C, Ctrl+V, Run, whack - and the output of the program is already before your reddish eyes.

We have selected the best online compilers: some of them are quite universal, others are tailored for strictly defined tasks. In any case, they will not be superfluous.

Koding

Koding.com is not an online compiler in the traditional sense. Each service user can create several full-fledged virtual machines running Ubuntu 14.04, on which he can do whatever he wants, including compiling code. All popular languages ​​are supported by default, but you can easily add your own.

In addition to the control panel for your server, a convenient IDE and a terminal window are available in the interface. Koding is the most universal tool; next we will look at simpler and more specialized options.

IdeaOne

IdeOne is an online compiler and debugging tool that allows you to execute code in over 60 programming languages ​​and their specific versions directly in the browser.

For those who do not have a girlfriend, the creators have provided code compilation in the Brainfuck language.

JDoodle

Another online compiler that supports many languages, including some that you won't find in many other online compilers. A nice feature of JDoodle is the ability to collaborate - just send a link to your current session and generate bugs at double speed!

jsFiddle

Don't let the name fool you - jsFiddle isn't just built for JavaScript. This online front-end editor allows you to test any combination of JavaScript, HTML and CSS. Of course, there is support for various frameworks, for example, jQuery, Vue, React, TypeScript, as well as CSS preprocessors like SCSS. For convenience, you can select a key binding from your favorite editor. True, only if your favorite editor is Vim, Emacs or Sublime Text.

CodePad

CodePad is a minimalistic service in which you can store code, share it, and run it with subsequent output of the results of its execution. There are several of the most common languages ​​to choose from, but, unfortunately, no choice of specific versions of interpreters or compilers.

Its main advantage is simplicity and ease: the site will work quickly even with slow internet. Auto-connection of standard headers is provided, as well as integration with Vim or Emacs.

One of the disadvantages is the complete lack of syntax highlighting when entering code into the form. However, when viewing an already saved recording, the backlight is present.

GCC GodBolt

GCC GodBolt is an interactive C++ compiler. I got into this collection for the reason that it has a simple interface, as well as a large number of settings, including options that can be adjusted using keys.

There are many compiler versions to choose from, including the latest ones. From interesting features you can note instant transfer program code in assembly language.