Runing a cpp binary on php could speed up my code?
I'm a php developer as well as cpp developer. I was wondering: if I make a cpp binary and I run it on php. Will that make my process run faster?
For example:
I have to compare 1,000 array elements and ex开发者_StackOverflowecute a process for each of them and in some cases I had to run it over and over again ( recursively) . Yes is messup but it works !.
- Yes, this might be faster. It's also very hard to do right (lots of corner cases in IPC).
- Don't try this unless it's absolutely necessary for performance. First try to improve the algorithm in PHP.
- Don't use the C++ code in production until you've measured the difference, and the C++ solution is significantly faster.
- Don't run a binary, write a library and link it into the PHP interpreter. PHP is implemented in C, so export your C++ functions to C using
extern "C"
.
I never did that in php, but in python I can tell you that it's a hell of a good way to squeeze performance. But don't overdo it: just implement in C what you know is a bottleneck, otherwise you will just create a monster.
Be sure to profile your code first and make sure you've actually identified the bottleneck. If its working now, it should be easy to include XDebug in your code so that you can measure its performance and profile your function calls. Maybe your function call isn't the bottleneck, in which case all your work would be wasted.
After that, see if there are any architectural issues before you switch languages. If there is a scalability problem, switching over to a faster language will just delay the issue.
精彩评论