I have a lot of mathematical calculations in my PHP script, which can be quite slow at times. I\'m wondering if it\'s possible to pass data from PHP to C++, do the calculati
If you want to handle these asynchronously, you could take a look at Beanstalkd, which acts as a job queue, storing chunks of data to be picked up by another process for processing. So you could write a C++ process that sits there waiting for something to do, and picks up work from Beanstalkd. Beanstalkd is not only fast, but can also be distributed.
There are a couple of C(++) client libraries for beanstalkd available https://github.com/kr/beanstalkd/wiki/client-libraries.
You can write a PHP module, which is a piece of C code that exposes a new built-in function to the PHP interpreter. This will be much faster than spinning up a second process via exec()
.
However, such modules are difficult to write, the build tools are clumsy, and passing parameters back and forth between PHP and C requires great care around memory allocation and reference counting. Also, C is not C++, so you will need an extern C
layer around your C++ code to export functions to PHP. (Or else write your extension in C instead of C++.)
So unless your speed requirement is really severe, it will be easier to use exec
or proc_open
and pass data back and forth through file pointers. This would be just like writing a C++ program that reads from standard input and emits to standard output.
If you run the calculations not so often than I'd go as Oleksi says; Use PHP's exec() command to run the external C++ app which will do the work.
But, if you run the calculations intensively, like a few times per second, then the C++ app's initial execution time might incur penalties;
Then you could look at having a C++ service/app which listens on a socket or a file-pipe, from/to which you pass data between PHP and C++. This will save the EXE's startup time.
For *ux systems you may launch you C++ application as CGI script in apache for example.
Apache: C/C++ script (might be available on windows also, as I see in this reference).
In this case you simply set the HMTL form to post (for example) on the CGI script.
I have tested in Linux and Apache 2, it is extremely easy coordination by HTTP.
From what i know the best solution would be writing PHP extension in C++.
This gives you possibility to create your own 'native' PHP function which you will name my_func(). And it will do all calculations you need with speed of C++ application and return you result as output from my_func().
You may want to look at this question: How to start writing a PHP5 extension in C++
You can do this, and this is a common solution to improve performance of performance-critical code. You can create a command line application in C++, and execute it using PHP's exec
command (or something similar).
As for passing data, you have a few options. If it's a lot of data, you can put it in a file using PHP, and access that file from C++. If it's less data, you can simply pass in command line arguments when you run the C++ program.