3v4l.org

run code in 500+ PHP versions simultaneously
<?php declare(strict_types=1); function totalRegistradosLenta(array $clientes, array $pedidos): array { $totales = []; foreach ($pedidos as $p) { if (in_array($p['rut'], array_column($clientes, 'rut'), true)) { $totales[$p['rut']] = ($totales[$p['rut']] ?? 0) + $p['monto']; } } return $totales; } function totalRegistradosRapida(array $clientes, array $pedidos): array { $registrados = array_flip(array_column($clientes, 'rut')); // una sola vez $totales = []; foreach ($pedidos as $p) { if (isset($registrados[$p['rut']])) { $totales[$p['rut']] = ($totales[$p['rut']] ?? 0) + $p['monto']; } } return $totales; } function verificar(string $caso, mixed $esperado, mixed $obtenido): void { printf("[%s] %s\n", $esperado === $obtenido ? ' OK ' : 'FALLA', $caso); } function generar(int $n): array { $clientes = $pedidos = []; for ($i = 1; $i <= $n; $i++) { $clientes[] = ['rut' => "{$i}-K", 'nombre' => "Cliente {$i}"]; $pedidos[] = ['rut' => rand(1, (int) ($n * 1.2)) . '-K', 'monto' => rand(500, 20000)]; } return [$clientes, $pedidos]; } [$c, $p] = generar(500); verificar('lenta y rápida dan lo mismo', totalRegistradosLenta($c, $p), totalRegistradosRapida($c, $p)); foreach ([1000, 2000, 4000] as $n) { [$c, $p] = generar($n); $t0 = hrtime(true); totalRegistradosLenta($c, $p); $t1 = hrtime(true); totalRegistradosRapida($c, $p); $t2 = hrtime(true); printf("n=%5d lenta: %8.2f ms rápida: %6.2f ms\n", $n, ($t1-$t0)/1e6, ($t2-$t1)/1e6); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 22, Position 2 = 62
Branch analysis from position: 22
2 jumps found. (Code = 78) Position 1 = 23, Position 2 = 62
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
Branch analysis from position: 62
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 62
filename:       /in/qjonA
function name:  (null)
number of ops:  64
compiled vars:  !0 = $c, !1 = $p, !2 = $n, !3 = $t0, !4 = $t1, !5 = $t2
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   42     0  E >   INIT_FCALL                                                   'generar'
          1        SEND_VAL                                                     500
          2        DO_FCALL                                          0  $6      
          3        FETCH_LIST_R                                         $7      $6, 0
          4        ASSIGN                                                       !0, $7
          5        FETCH_LIST_R                                         $9      $6, 1
          6        ASSIGN                                                       !1, $9
          7        FREE                                                         $6
   43     8        INIT_FCALL                                                   'verificar'
          9        SEND_VAL                                                     'lenta+y+r%C3%A1pida+dan+lo+mismo'
         10        INIT_FCALL                                                   'totalregistradoslenta'
         11        SEND_VAR                                                     !0
         12        SEND_VAR                                                     !1
         13        DO_FCALL                                          0  $11     
         14        SEND_VAR                                                     $11
         15        INIT_FCALL                                                   'totalregistradosrapida'
         16        SEND_VAR                                                     !0
         17        SEND_VAR                                                     !1
         18        DO_FCALL                                          0  $12     
         19        SEND_VAR                                                     $12
         20        DO_FCALL                                          0          
   45    21      > FE_RESET_R                                           $14     <array>, ->62
         22    > > FE_FETCH_R                                                   $14, !2, ->62
   46    23    >   INIT_FCALL                                                   'generar'
         24        SEND_VAR                                                     !2
         25        DO_FCALL                                          0  $15     
         26        FETCH_LIST_R                                         $16     $15, 0
         27        ASSIGN                                                       !0, $16
         28        FETCH_LIST_R                                         $18     $15, 1
         29        ASSIGN                                                       !1, $18
         30        FREE                                                         $15
   47    31        INIT_FCALL                                                   'hrtime'
         32        SEND_VAL                                                     <true>
         33        DO_ICALL                                             $20     
         34        ASSIGN                                                       !3, $20
         35        INIT_FCALL                                                   'totalregistradoslenta'
         36        SEND_VAR                                                     !0
         37        SEND_VAR                                                     !1
         38        DO_FCALL                                          0          
   48    39        INIT_FCALL                                                   'hrtime'
         40        SEND_VAL                                                     <true>
         41        DO_ICALL                                             $23     
         42        ASSIGN                                                       !4, $23
         43        INIT_FCALL                                                   'totalregistradosrapida'
         44        SEND_VAR                                                     !0
         45        SEND_VAR                                                     !1
         46        DO_FCALL                                          0          
   49    47        INIT_FCALL                                                   'hrtime'
         48        SEND_VAL                                                     <true>
         49        DO_ICALL                                             $26     
         50        ASSIGN                                                       !5, $26
   50    51        INIT_FCALL                                                   'printf'
         52        SEND_VAL                                                     'n%3D%255d++lenta%3A+%258.2f+ms+++r%C3%A1pida%3A+%256.2f+ms%0A'
         53        SEND_VAR                                                     !2
         54        SUB                                                  ~28     !4, !3
         55        DIV                                                  ~29     ~28, 1.0e+6
         56        SEND_VAL                                                     ~29
         57        SUB                                                  ~30     !5, !4
         58        DIV                                                  ~31     ~30, 1.0e+6
         59        SEND_VAL                                                     ~31
         60        DO_ICALL                                                     
   45    61      > JMP                                                          ->22
         62    >   FE_FREE                                                      $14
   51    63      > RETURN                                                       1

Function totalregistradoslenta:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 23
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 23
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 22
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 22
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/qjonA
function name:  totalRegistradosLenta
number of ops:  28
compiled vars:  !0 = $clientes, !1 = $pedidos, !2 = $totales, !3 = $p
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    4     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
    6     2        ASSIGN                                                       !2, <array>
    7     3      > FE_RESET_R                                           $5      !1, ->23
          4    > > FE_FETCH_R                                                   $5, !3, ->23
    8     5    >   FETCH_DIM_R                                          ~6      !3, 'rut'
          6        INIT_FCALL                                                   'array_column'
          7        SEND_VAR                                                     !0
          8        SEND_VAL                                                     'rut'
          9        DO_ICALL                                             $7      
         10        FRAMELESS_ICALL_3                in_array            ~8      ~6, $7
         11        OP_DATA                                                      <true>
         12      > JMPZ                                                         ~8, ->22
    9    13    >   FETCH_DIM_R                                          ~9      !3, 'rut'
         14        FETCH_DIM_R                                          ~11     !3, 'rut'
         15        FETCH_DIM_IS                                         ~12     !2, ~11
         16        COALESCE                                             ~13     ~12
         17        QM_ASSIGN                                            ~13     0
         18        FETCH_DIM_R                                          ~14     !3, 'monto'
         19        ADD                                                  ~15     ~13, ~14
         20        ASSIGN_DIM                                                   !2, ~9
         21        OP_DATA                                                      ~15
    7    22    > > JMP                                                          ->4
         23    >   FE_FREE                                                      $5
   12    24        VERIFY_RETURN_TYPE                                           !2
         25      > RETURN                                                       !2
   13    26*       VERIFY_RETURN_TYPE                                           
         27*     > RETURN                                                       null

End of function totalregistradoslenta

Function totalregistradosrapida:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 26
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 26
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 25
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 25
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
filename:       /in/qjonA
function name:  totalRegistradosRapida
number of ops:  31
compiled vars:  !0 = $clientes, !1 = $pedidos, !2 = $registrados, !3 = $totales, !4 = $p
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   15     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   17     2        INIT_FCALL                                                   'array_flip'
          3        INIT_FCALL                                                   'array_column'
          4        SEND_VAR                                                     !0
          5        SEND_VAL                                                     'rut'
          6        DO_ICALL                                             $5      
          7        SEND_VAR                                                     $5
          8        DO_ICALL                                             $6      
          9        ASSIGN                                                       !2, $6
   18    10        ASSIGN                                                       !3, <array>
   19    11      > FE_RESET_R                                           $9      !1, ->26
         12    > > FE_FETCH_R                                                   $9, !4, ->26
   20    13    >   FETCH_DIM_R                                          ~10     !4, 'rut'
         14        ISSET_ISEMPTY_DIM_OBJ                             0          !2, ~10
         15      > JMPZ                                                         ~11, ->25
   21    16    >   FETCH_DIM_R                                          ~12     !4, 'rut'
         17        FETCH_DIM_R                                          ~14     !4, 'rut'
         18        FETCH_DIM_IS                                         ~15     !3, ~14
         19        COALESCE                                             ~16     ~15
         20        QM_ASSIGN                                            ~16     0
         21        FETCH_DIM_R                                          ~17     !4, 'monto'
         22        ADD                                                  ~18     ~16, ~17
         23        ASSIGN_DIM                                                   !3, ~12
         24        OP_DATA                                                      ~18
   19    25    > > JMP                                                          ->12
         26    >   FE_FREE                                                      $9
   24    27        VERIFY_RETURN_TYPE                                           !3
         28      > RETURN                                                       !3
   25    29*       VERIFY_RETURN_TYPE                                           
         30*     > RETURN                                                       null

End of function totalregistradosrapida

Function verificar:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qjonA
function name:  verificar
number of ops:  14
compiled vars:  !0 = $caso, !1 = $esperado, !2 = $obtenido
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   27     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV                                                 !2      
   29     3        INIT_FCALL                                                   'printf'
          4        SEND_VAL                                                     '%5B%25s%5D+%25s%0A'
          5        IS_IDENTICAL                                                 !1, !2
          6      > JMPZ                                                         ~3, ->9
          7    >   QM_ASSIGN                                            ~4      '+OK+'
          8      > JMP                                                          ->10
          9    >   QM_ASSIGN                                            ~4      'FALLA'
         10    >   SEND_VAL                                                     ~4
         11        SEND_VAR                                                     !0
         12        DO_ICALL                                                     
   30    13      > RETURN                                                       null

End of function verificar

Function generar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 5
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 5
Branch analysis from position: 31
Branch analysis from position: 5
filename:       /in/qjonA
function name:  generar
number of ops:  37
compiled vars:  !0 = $n, !1 = $clientes, !2 = $pedidos, !3 = $i
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   32     0  E >   RECV                                                 !0      
   34     1        ASSIGN                                               ~4      !2, <array>
          2        ASSIGN                                                       !1, ~4
   35     3        ASSIGN                                                       !3, 1
          4      > JMP                                                          ->29
   36     5    >   NOP                                                          
          6        FAST_CONCAT                                          ~8      !3, '-K'
          7        INIT_ARRAY                                           ~9      ~8, 'rut'
          8        NOP                                                          
          9        FAST_CONCAT                                          ~10     'Cliente+', !3
         10        ADD_ARRAY_ELEMENT                                    ~9      ~10, 'nombre'
         11        ASSIGN_DIM                                                   !1
         12        OP_DATA                                                      ~9
   37    13        INIT_FCALL                                                   'rand'
         14        SEND_VAL                                                     1
         15        MUL                                                  ~12     !0, 1.2
         16        CAST                                              4  ~13     ~12
         17        SEND_VAL                                                     ~13
         18        DO_ICALL                                             $14     
         19        CONCAT                                               ~15     $14, '-K'
         20        INIT_ARRAY                                           ~16     ~15, 'rut'
         21        INIT_FCALL                                                   'rand'
         22        SEND_VAL                                                     500
         23        SEND_VAL                                                     20000
         24        DO_ICALL                                             $17     
         25        ADD_ARRAY_ELEMENT                                    ~16     $17, 'monto'
         26        ASSIGN_DIM                                                   !2
         27        OP_DATA                                                      ~16
   35    28        PRE_INC                                                      !3
         29    >   IS_SMALLER_OR_EQUAL                                          !3, !0
         30      > JMPNZ                                                        ~19, ->5
   39    31    >   INIT_ARRAY                                           ~20     !1
         32        ADD_ARRAY_ELEMENT                                    ~20     !2
         33        VERIFY_RETURN_TYPE                                           ~20
         34      > RETURN                                                       ~20
   40    35*       VERIFY_RETURN_TYPE                                           
         36*     > RETURN                                                       null

End of function generar

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
142.24 ms | 502 KiB | 19 Q