Is using functions like parameters for other functions slower?

Asked

Viewed 110 times

1

I’m taking information from a site that generates content in format JSON and a question arose about using functions as parameters of other functions, this slows down the application? and if so, how to know?

Example:

$requestUrl = 'BlaBla.com';
$pageContent = json_decode(file_get_contents($requestUrl), true);

OR

$pageContent = file_get_contents($requestUrl);
$decodeContent = json_decode($pageContent, true);
  • Henry, I’m sorry, but... where is the function as a function parameter? Here json_decode(file_get_contents($requestUrl), true);?

  • passed file_get_contents() as parameter to json_decode

  • So... just for explanation, I wouldn’t answer your question, but it’s something useful, you’re not passing the function file_get_contents() for json_decode(), but yes is passing the function return file_get_contents() as a function parameter json_decode(). Understands?

  • Then first the code block of the inner function will be processed, and then return will be sent as parameter to the outer function.

  • Ah, got it, thanks for clarifying!

1 answer

2


No. In your example the time difference is insignificant, don’t worry about it. I believe a significant difference will only be noticed in recursive functions that abuse function calls. If you really want to test this, use the code below.

$start = microtime(true);

$requestUrl = 'BlaBla.com';
$pageContent = json_decode(file_get_contents($requestUrl), true);

$end = microtime(true);
$time = number_format(($end - $start), 2);

echo 'Tempo de execução ', $time, ' segundos';
  • Not to mention that the execution time can vary a lot, of several factors, mainly in developing machines where the php process will compete with other processes. The difference would be in memory allocation, one placed in variable and the other output directly to the other function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.