0

Let's say I want to fetch contents from a URL using a PHP script.

One way to do that would be to use PHP function such as

echo file_get_contents("http://www.example.com/file.xml");

Another way would be to use UNIX tools such as wget or curl, or any other tool accessible from shell

echo exec("wget http://www.example.com/file.xml");

Is there a significant performance difference between using exec() and PHP build-in functions to achieve same thing, assuming that both UNIX tools and PHP functions have similar implementation and perform with same efficiency?

What exactly happens when you call an exec() function in terms of resources? Does it actually create a new shell session, or does it run on top of the current php shell session?

5
  • a new process is created from scratch. I doubt you'll see significant difference in performance because this operation is dominated by external io. Commented Jul 13, 2013 at 3:42
  • Can you please elaborate? What do you mean by operation being dominated by external IO? Commented Jul 13, 2013 at 3:45
  • 1
    for what its worth my rule of thumb is to do it in php if it has the functions for it (in the above example curl is probably the best option), better control, error checking etc. Commented Jul 13, 2013 at 3:45
  • Also exec is sometimes blocked on shared hosting, and would have a bigger security risk if user input is taken. (There shouldn't be any risk if you script it right though) Commented Jul 13, 2013 at 3:47
  • Try both of them and time them and see for yourself. That's the only answer that really matters. Shelling out will probably be slower, but if the PHP takes 1500ms and the shelled out version takes 1540ms, the speed difference is negligible. Commented Jul 13, 2013 at 4:58

1 Answer 1

3

The exec() function creates a new shell instance, with its own environment variables, so there is a performance hit.

Sign up to request clarification or add additional context in comments.

1 Comment

However, without quantifying that performance hit, it's impossible to say if it's worth worrying about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.