views:

92

answers:

5

Hi,

I need to make calls from webpage to external library written in C++ and display the result. Platform is Linux, Apache, PHP.

My current idea is to use PHP service which will call my library/program. I found that there are two possible ways to do this: 1) use PHP 'exec' function 2) write PHP extension

I am curious what works more effective? Faster? Less load the server?

I will probably need to do 4 calls per second, so I want to be as optimal as possible.

P.S. If you are aware of some other (more effective) way of calling C++ library or program from webpage, please let me know.

Thanks a lot,
Robusta

+4  A: 
Jauzsika
No they are not always slower. Certainly there's usually more latency in invoking them but at the cost of poorer performance elsewhere, and that's before you start to consider the cases where they may be intrinsically slower as a result of running as a session. At least Artefacto qualified his response as 'theoretically'. However we'd need to know an awful lot more about what the code does to be able to make an informed guess.
symcbean
+1 for the benchmark effort. My "theoretically" qualifier should be read as "I haven't done any tests" :p
Artefacto
+7  A: 

An extension is theoretically faster because it avoids the overhead of creating a new process. It's also a "cleaner" solution (no awkward program arguments escaping; you are able to parse arbitrary PHP values such as objects instead of only strings, etc.).

However, if you already have a command line program that uses that library, it will be easier for you to just execute it instead of writing an extension.

Note that if you only make 4 calls per second, performance-wise it's indifferent which method you use, unless your library requires expensive initialization that can be avoided by having persistent (cross-request) state stored in a PHP extension.

Artefacto
+1  A: 

If you want to spare yourself the hassle of writting a php extension (exec is too slow) you might want to write some kind of webservice of the library you are using (e.g. with XML-RPC or SOAP/REST) and call this from your php code.

This should be easier to debug (Simply log the requests you made and replay them) as well as easier to separate (need to run one part on a different host for various reasons? :P)

ZeissS
A web service is however much slower than calling exec.
Emil Vikström
Why does it have to be a webservice? It could be any sort of daemon.
symcbean
A: 

As for as I am concerning WebService is much slower than exe program. But is having the advantage of platform independent as ZeisS said.

And I strongly agree with Emil Vikstrom . Consider an example, in your house(system) you(OS) will give more preference for family members(exe) than guests(webservice). In case of recursive functions, exe will run much faster than webservices.

prabhakaran
A: 

Speed vs. Portability. If you link another library against PHP, you have to keep it updated; hence need a build system or better yet a custom package for your distribution. This is practically not a big deal. But it's still simpler to just use a commandline utility when available.

So, if it is a library that can be used in a session-like fashion (multiple calls, adapt environment) you should definitely build a wrapper. If you don't invoke it >10 times per second, the speed difference is negligible. Go for the execve() method then. It's the Unix way and factually more robust if your application has a certain lifetime expectation.

mario