How to make PHP faster: is there a line creation?

A bit of code is presented in this question , and the questionnaire wants to do it faster by eliminating the use of variables. He seems to be looking in the wrong place, but far from me to know. Here is the code

while ($item = current($data))
{
    echo '<ATTR>',$item, '</ATTR>', "\n";
    next($data);
}

It seems to me that recreating strings <ATTR>, etc. - more than once on each line and every time the line is processed - would have a cost associated with them (both in terms of speed and memory), Or maybe the PHP processor is smart enough that there is no penalty for that he did not put strings in variables before the loop?

I use variables for clarity and centralization anyway, but: is there a cost associated with using variables and not using variables or what? (Anyone who wants to be responsible for other similar languages , please feel free to.)

+3
source share
6 answers

If you really want to micro-optimize this method (I don’t think it is relevant or useful, by the way, but I understand it is fun ^^), you can take a look at the PHP extension called Vulcan Logic Disassembler

It allows you to get the bytecode generated for the PHP script.

, , , script:

php -dextension=vld.so -dvld.active=1 tests/temp/temp.php

, script:

$data = array('a', 'b', 'c', 'd');
while ($item = current($data))
{
    echo '<ATTR>',$item, '</ATTR>', "\n";
    next($data);
}

-:

line     #  op                           fetch          ext  return  operands
-------------------------------------------------------------------------------
   8     0  EXT_STMT
         1  INIT_ARRAY                                       ~0      'a'
         2  ADD_ARRAY_ELEMENT                                ~0      'b'
         3  ADD_ARRAY_ELEMENT                                ~0      'c'
         4  ADD_ARRAY_ELEMENT                                ~0      'd'
         5  ASSIGN                                                   !0, ~0
   9     6  EXT_STMT
         7  EXT_FCALL_BEGIN
         8  SEND_REF                                                 !0
         9  DO_FCALL                                      1          'current'
        10  EXT_FCALL_END
        11  ASSIGN                                           $3      !1, $2
        12  JMPZ                                                     $3, ->24
  11    13  EXT_STMT
        14  ECHO                                                     '%3CATTR%3E'
        15  ECHO                                                     !1
        16  ECHO                                                     '%3C%2FATTR%3E'
        17  ECHO                                                     '%0A'
  12    18  EXT_STMT
        19  EXT_FCALL_BEGIN
        20  SEND_REF                                                 !0
        21  DO_FCALL                                      1          'next'
        22  EXT_FCALL_END
  13    23  JMP                                                      ->7
  37    24  RETURN                                                   1
        25* ZEND_HANDLE_EXCEPTION

script:

$data = array('a', 'b', 'c', 'd');
while ($item = current($data))
{
    echo "<ATTR>$item</ATTR>\n";
    next($data);
}

:

line     #  op                           fetch          ext  return  operands
-------------------------------------------------------------------------------
  19     0  EXT_STMT
         1  INIT_ARRAY                                       ~0      'a'
         2  ADD_ARRAY_ELEMENT                                ~0      'b'
         3  ADD_ARRAY_ELEMENT                                ~0      'c'
         4  ADD_ARRAY_ELEMENT                                ~0      'd'
         5  ASSIGN                                                   !0, ~0
  20     6  EXT_STMT
         7  EXT_FCALL_BEGIN
         8  SEND_REF                                                 !0
         9  DO_FCALL                                      1          'current'
        10  EXT_FCALL_END
        11  ASSIGN                                           $3      !1, $2
        12  JMPZ                                                     $3, ->25
  22    13  EXT_STMT
        14  INIT_STRING                                      ~4
        15  ADD_STRING                                       ~4      ~4, '%3CATTR%3E'
        16  ADD_VAR                                          ~4      ~4, !1
        17  ADD_STRING                                       ~4      ~4, '%3C%2FATTR%3E%0A'
        18  ECHO                                                     ~4
  23    19  EXT_STMT
        20  EXT_FCALL_BEGIN
        21  SEND_REF                                                 !0
        22  DO_FCALL                                      1          'next'
        23  EXT_FCALL_END
  24    24  JMP                                                      ->7
  39    25  RETURN                                                   1
        26* ZEND_HANDLE_EXCEPTION

( PHP 5.2.6, Ubuntu Jaunty)

, , , , , micro -optimisation ^^

, PHP: , PHP 5.1 5.2, .

Opcodes

!

EDIT: :

:

$attr_open = '<ATTR>';
$attr_close = '</ATTR>';
$eol = "\n";

$data = array('a', 'b', 'c', 'd');
while ($item = current($data))
{
    echo $attr_open, $item, $attr_close, $eol;
    next($data);
}

:

line     #  op                           fetch          ext  return  operands
-------------------------------------------------------------------------------
  19     0  EXT_STMT
         1  ASSIGN                                                   !0, '%3CATTR%3E'
  20     2  EXT_STMT
         3  ASSIGN                                                   !1, '%3C%2FATTR%3E'
  21     4  EXT_STMT
         5  ASSIGN                                                   !2, '%0A'
  23     6  EXT_STMT
         7  INIT_ARRAY                                       ~3      'a'
         8  ADD_ARRAY_ELEMENT                                ~3      'b'
         9  ADD_ARRAY_ELEMENT                                ~3      'c'
        10  ADD_ARRAY_ELEMENT                                ~3      'd'
        11  ASSIGN                                                   !3, ~3
  24    12  EXT_STMT
        13  EXT_FCALL_BEGIN
        14  SEND_REF                                                 !3
        15  DO_FCALL                                      1          'current'
        16  EXT_FCALL_END
        17  ASSIGN                                           $6      !4, $5
        18  JMPZ                                                     $6, ->30
  26    19  EXT_STMT
        20  ECHO                                                     !0
        21  ECHO                                                     !4
        22  ECHO                                                     !1
        23  ECHO                                                     !2
  27    24  EXT_STMT
        25  EXT_FCALL_BEGIN
        26  SEND_REF                                                 !3
        27  DO_FCALL                                      1          'next'
        28  EXT_FCALL_END
  28    29  JMP                                                      ->13
  43    30  RETURN                                                   1
        31* ZEND_HANDLE_EXCEPTION

( ','):

$attr_open = '<ATTR>';
$attr_close = '</ATTR>';
$eol = "\n";

$data = array('a', 'b', 'c', 'd');
while ($item = current($data))
{
    echo $attr_open . $item . $attr_close . $eol;
    next($data);
}

:

line     #  op                           fetch          ext  return  operands
-------------------------------------------------------------------------------
  19     0  EXT_STMT
         1  ASSIGN                                                   !0, '%3CATTR%3E'
  20     2  EXT_STMT
         3  ASSIGN                                                   !1, '%3C%2FATTR%3E'
  21     4  EXT_STMT
         5  ASSIGN                                                   !2, '%0A'
  23     6  EXT_STMT
         7  INIT_ARRAY                                       ~3      'a'
         8  ADD_ARRAY_ELEMENT                                ~3      'b'
         9  ADD_ARRAY_ELEMENT                                ~3      'c'
        10  ADD_ARRAY_ELEMENT                                ~3      'd'
        11  ASSIGN                                                   !3, ~3
  24    12  EXT_STMT
        13  EXT_FCALL_BEGIN
        14  SEND_REF                                                 !3
        15  DO_FCALL                                      1          'current'
        16  EXT_FCALL_END
        17  ASSIGN                                           $6      !4, $5
        18  JMPZ                                                     $6, ->30
  26    19  EXT_STMT
        20  CONCAT                                           ~7      !0, !4
        21  CONCAT                                           ~8      ~7, !1
        22  CONCAT                                           ~9      ~8, !2
        23  ECHO                                                     ~9
  27    24  EXT_STMT
        25  EXT_FCALL_BEGIN
        26  SEND_REF                                                 !3
        27  DO_FCALL                                      1          'next'
        28  EXT_FCALL_END
  28    29  JMP                                                      ->13
  43    30  RETURN                                                   1
        31* ZEND_HANDLE_EXCEPTION

, ^^

+3

, , , , char PHP, ... .

$nl = "\n";
while ($item = current($data))
{
    echo '<ATTR>',$item, '</ATTR>',$nl;
    next($data);
}
+3

, . script, :

$length = 100000;
$data = array();
$totals = array();

for ($i = 0; $i < $length; $i++) {
    $data[] = rand(1,1000);
}

$start = xdebug\_time\_index();
while ($item = current($data))
{
    echo '<ATTR>',$item,'</ATTR>',PHP_EOL;
    next($data);
}
$end = xdebug\_time\_index();
$total = $end - $start;
$totals["Warmup:"] = $total;

reset($data);
$start = xdebug\_time\_index();
while ($item = current($data))
{
    echo '<ATTR>',$item,'</ATTR>',PHP_EOL;
    next($data);
}
$end = xdebug\_time\_index();
$total = $end - $start;
$totals["First:"] = $total;

reset($data);
$startTag = '<ATTR>';
$endTag = '</ATTR>';
$start = xdebug\_time\_index();
while ($item = current($data))
{
    echo $startTag,$item,$endTag,PHP_EOL;
    next($data);
}
$end = xdebug\_time\_index();
$total = $end - $start;
$totals["Second:"] = $total;

foreach ($totals as $label => $data) {
    echo $label,' ', $data,PHP_EOL;
}

. , .

, , , . , PHP_EOL \n , .

+2

, , . , concat .

+1

. , .

, . , .

+1

, :

ob_start();
while ($item = current($data))
{
    echo '<ATTR>',$item, '</ATTR>', "\n";
    next($data);
}

, , -.

Aside, in my experience, micro-optimization is a worthless job when it comes to PHP code. I have never seen a performance problem be solved by cleverly using a specific concatenation method or declaring a variable. Real decisions are usually associated with a change in design or architecture or the use of less complex algorithms.

+1
source

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


All Articles