📜 ⬆️ ⬇️

Is static needed?

Quite often recently I read various articles on optimization, many of them wrote that if a function is used as static, then from an optimization point of view it is better to write a static modifier before declaring it.

Interesting, I thought, and what a real performance boost can be obtained by adding 6 characters to the function declaration.

So the initial configuration
Server: 2 x Intel Pentium D CPU 3.00GHz (although the second processor was not much needed)
PHP: PHP 5.2.5 (cli) (built: Apr 9 2008 06:17:23)

The first thing that comes to mind as a test is the function of calculating the factorial of a number. In the example, I took the calculation of factorial 30 as a test (why 30 is described in the second example). Sample test (do not pay attention to the idiotic timer), multiplication by 1,000,000 was chosen to improve the perception of the results
')
I apologize, but the editor could not be defeated, for a correct reproduction of the sign is less, instead of it is a bracket.

class Math
{
function fac ($ v)
{
return ($ v == 0 || $ v == 1)? 1: ($ v * Math :: fac ($ v-1));
}
static function sfac ($ v)
{
return ($ v == 0 || $ v == 1)? 1: ($ v * Math :: sfac ($ v-1));
}
}
function gettime ()
{
$ arr = explode ('', microtime ());
return $ arr [1] + $ arr [0];
}
for ($ i = 1; $ i [= 100; ++ $ i)
{
echo $ i. "\ t";
$ v = gettime ();
for ($ j = 0; $ j [$ i; ++ $ j)
Math :: fac (30);
echo ((gettime () - $ v) / $ i * 1,000,000). "\ t";

$ v = gettime ();
for ($ j = 0; $ j [$ i; ++ $ j)
Math :: sfac (30);
echo ((gettime () - $ v) / $ i * 1,000,000). "\ n";
}

results

The average operation time of the function declared without the static modifier is 64.518451690674 microseconds.
The average running time of the function declared with the static modifier is 31.158924102783 microseconds

Then I wanted to choose some complex calculations. I took the calculation of 30 members of the Fibonacci sequence . Deep enough recursion. Sample test (do not pay attention to the idiotic timer)

class Math
{
function fib ($ v)
{
return ($ v == 0 || $ v == 1)? 1: (Math :: fib ($ v-1) + Math :: fib ($ v-2));
}
static function sfib ($ v)
{
return ($ v == 0 || $ v == 1)? 1: (Math :: sfib ($ v-1) + Math :: sfib ($ v-2));
}
}
for ($ i = 1; $ i [= 100; ++ $ i)
{
echo $ i. "\ t";
$ v = gettime ();
for ($ j = 0; $ j [$ i; ++ $ j)
Math :: fib (30);
echo ((gettime () - $ v) / $ i). "\ t";

$ v = gettime ();
for ($ j = 0; $ j [$ i; ++ $ j)
Math :: sfib (30);
echo ((gettime () - $ v) / $ i). "\ n";
}

100 calculations were taken to see more accurately the mean

results

The average running time of the function declared without the static modifier is 5.5365417218208 seconds.
The average running time of the function declared with the static modifier is 2.6412206792831 seconds

What can we conclude, except that I spent 11 hours of CPU time :-).
If functions are frequently called, especially in recursive calculations, adding a static modifier will significantly increase performance.
However, if this is a simple function that is called only once, the performance increase will be negligible.

update: Many may ask why do 1 start in succession, then 2 start in succession, ..., then 100 start. It was possible to immediately run 100 iterations. Yes you can, but I simultaneously explored other things that have nothing to do with static

Source: https://habr.com/ru/post/23268/


All Articles