K
K
Katzuhiro_Akira2018-08-06 05:44:25
PHP
Katzuhiro_Akira, 2018-08-06 05:44:25

How does recursion affect performance?

Good day.
For some time of my experience, I wrote code in a procedural style, that is, there was no talk of any recursions and functions at all (only to get rid of repetitions).
Now a task has appeared that requires maximum universalization, so I had to slightly shift my style from familiar procedures to a functional style.
How will the task execution speed change depending on nesting?
For example, there is such a situation (it is more reasonable to write in code):

function func($arg1)
{
  switch(true)
  {
    case $arg1 == true:
      Некоторые дейсвия для положительного
    break;

    case $arg1 == false:
       Некоторые дейсвия для отрицательного
    break;

    default:
     func(true); // если не выбрано входящее
    break;
   }
};

Second case:
function func($arg1)
{
   if(func2($arg1)!=false)
    {
       // некая результирующая операция, вроде закрытия файла/потока/сессии
     };
};

In what cases will the performance of using recursion decrease relative to the full writing of procedures. I perfectly understand about the bulkiness (and the script, respectively) of procedures regarding functions and objects, but won't the call take more time than the execution of the same action by procedures?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavlo Ponomarenko, 2018-08-06
@TheShock

Well the recursion yet does not mean functionality. It's all the same procedural style.
Don't worry about performance - most likely this whole section of code will not be a source of brakes, and if it is, you will see it in the profiler and then you will think about how to optimize it

R
Rsa97, 2018-08-06
@Rsa97

In the second case, there is no recursion, unless, of course, func1 is called again from func2.
In the first case, everything is solved much easier

function func($arg1 = true)
{
  if ($arg1) {
    ...
  } else {
    ...
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question