开发者

Runing a cpp binary on php could speed up my code?

开发者 https://www.devze.com 2023-01-28 05:58 出处:网络
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?

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 !.


  1. Yes, this might be faster. It's also very hard to do right (lots of corner cases in IPC).
  2. Don't try this unless it's absolutely necessary for performance. First try to improve the algorithm in PHP.
  3. Don't use the C++ code in production until you've measured the difference, and the C++ solution is significantly faster.
  4. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消