Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exec() runs slow in PHP - same command runs much faster interactively

Can anyone tell me why exec() function is so slow and time unpredictable on different machines in PHP?

Basically I have some executable file and want to execute it through PHP:

$command = '/usr/pathToComman/myCommand -someParameters';
exec($command);

The thing is that it executes much longer (by much I mean 3-4 sometimes like 25 times longer) then the same command from the shell.

In additional to this the command is executed longer from the PHP on my server, which is stronger (more RAM and more GHz).

So there are two questions:

  • why?
  • what should I do with this?

P.S. I need this execute, because I can not do the same thing with PHP

P.S.2 Answering @prodigitalson question: It solves the differential equation, but basically no matter what it does, the speed is just much slower. I remember doing some image processing this way and the speed was also much slower.

like image 850
Salvador Dali Avatar asked Nov 13 '22 17:11

Salvador Dali


1 Answers

The problem why this is slower is probably because your PHP server has to go to the shell to run your command. So what the PHP server does then is create a new shell and call the command on the new shell. Creating a new shell means in most systems to create a new thread. So all those things together result in a longer execution time.

Sometimes a cgi script can help with those problems, but I'm not sure if it will help here (because I don't really know what you are trying to do on the shell)

like image 150
ndsmyter Avatar answered Nov 28 '22 06:11

ndsmyter