Reputation: 26085
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 calculations in C++, then pass the results back to PHP?
P.S. I'm not very good at C++.
Upvotes: 14
Views: 7525
Reputation: 1402
Yes it can be done:
PHP can run commands on the server.
The process is different for both WINDOWS and LINUX systems
For windows check these links
link for background processes in windows
For Linux:
Running a process in background
Well i tried to use this process and succeed a little.
And one more thing you can grab the results too
Upvotes: 1
Reputation: 2948
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++
Upvotes: 0
Reputation: 2434
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.
Upvotes: 0
Reputation: 3103
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.
Upvotes: 0
Reputation: 1
php function system() would do the work. Just pass compiled c++ file to the system() and get the returned result.
Upvotes: 0
Reputation: 709
To give another alternative, one that actually might even help you a great deal if you have to do multiple calculations at once that could run in parallel is to use Gearman.
Gearman will take care of the talking to C++ and you could run multiple calculations all at the same time. Gearman itself is simply a component that allows you to talk to it as a task server and you write workers that connect to it to execute the actual work. This also allows you to scale up, say you have something large to calculate, you could fire up 20 AWS EC2 instances and run the workers on there for the heavy lifting, then shut them down after it's done.
For best performance if it's straight forward and parallel execution is not needed, I would use Crashworks approach 100%, or you could mix the two.
Upvotes: 3
Reputation: 13116
You could always compile your code using Facebooks HipHop.
It will transform your php into C++ and then compile that to machine code using g++.
https://github.com/facebook/hiphop-php/wiki/
https://github.com/facebook/hiphop-php/wiki/Running-HipHop
I've never personally used it on a real project but I've heard great things.
Upvotes: 7
Reputation: 41394
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.
Upvotes: 20
Reputation: 11317
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.
Upvotes: 7
Reputation: 59997
Create an executable in C++. Use proc_code to pass data to/from that executable andd get that executable to do the calculations.
Upvotes: 0
Reputation: 13097
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.
Upvotes: 3