3v4l.org

run code in 300+ PHP versions simultaneously
<?php function BinSearch($needle, $haystack){ asort($haystack); $max = count($haystack); $min = $pos = 0; $mid = false; do { printf("\n\nSeeking for %d, found %d between %d and %d…", $needle, $haystack[$pos], $min, $max); if ( $needle === $haystack[$pos] ) { return $pos; } if ( $haystack[$pos] > $needle ) { $max = $pos; // we don't need to go any higher than $pos $pos = (int)max($min, $pos/2); printf("we're too high, coming down to %d", $pos); } else { $min = $pos; $pos = (int)min($max, $pos+($max - $pos) / 2); printf("we're too low, increasing to %d", $pos); } } while (($pos < $max) && ($haystack[$pos] !== $needle)); return $pos; } $x = range(0,999); $foundPosition = BinSearch($seek = 82,$x); echo "\n\n\nFound $seek at position $foundPosition.";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fnT7p
function name:  (null)
number of ops:  18
compiled vars:  !0 = $x, !1 = $foundPosition, !2 = $seek
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   INIT_FCALL                                               'range'
          1        SEND_VAL                                                 0
          2        SEND_VAL                                                 999
          3        DO_ICALL                                         $3      
          4        ASSIGN                                                   !0, $3
   28     5        INIT_FCALL                                               'binsearch'
          6        ASSIGN                                           ~5      !2, 82
          7        SEND_VAL                                                 ~5
          8        SEND_VAR                                                 !0
          9        DO_FCALL                                      0  $6      
         10        ASSIGN                                                   !1, $6
   29    11        ROPE_INIT                                     5  ~9      '%0A%0A%0AFound+'
         12        ROPE_ADD                                      1  ~9      ~9, !2
         13        ROPE_ADD                                      2  ~9      ~9, '+at+position+'
         14        ROPE_ADD                                      3  ~9      ~9, !1
         15        ROPE_END                                      4  ~8      ~9, '.'
         16        ECHO                                                     ~8
         17      > RETURN                                                   1

Function binsearch:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 22
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 38
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
2 jumps found. (Code = 46) Position 1 = 54, Position 2 = 57
Branch analysis from position: 54
2 jumps found. (Code = 44) Position 1 = 58, Position 2 = 10
Branch analysis from position: 58
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
Branch analysis from position: 57
Branch analysis from position: 38
2 jumps found. (Code = 46) Position 1 = 54, Position 2 = 57
Branch analysis from position: 54
Branch analysis from position: 57
filename:       /in/fnT7p
function name:  BinSearch
number of ops:  60
compiled vars:  !0 = $needle, !1 = $haystack, !2 = $max, !3 = $min, !4 = $pos, !5 = $mid
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    4     2        INIT_FCALL                                               'asort'
          3        SEND_REF                                                 !1
          4        DO_ICALL                                                 
    5     5        COUNT                                            ~7      !1
          6        ASSIGN                                                   !2, ~7
    6     7        ASSIGN                                           ~9      !4, 0
          8        ASSIGN                                                   !3, ~9
    7     9        ASSIGN                                                   !5, <false>
    9    10    >   INIT_FCALL                                               'printf'
         11        SEND_VAL                                                 '%0A%0ASeeking+for+%25d%2C+found+%25d+between+%25d+and+%25d%E2%80%A6'
         12        SEND_VAR                                                 !0
         13        FETCH_DIM_R                                      ~12     !1, !4
         14        SEND_VAL                                                 ~12
         15        SEND_VAR                                                 !3
         16        SEND_VAR                                                 !2
         17        DO_ICALL                                                 
   10    18        FETCH_DIM_R                                      ~14     !1, !4
         19        IS_IDENTICAL                                             !0, ~14
         20      > JMPZ                                                     ~15, ->22
   11    21    > > RETURN                                                   !4
   14    22    >   FETCH_DIM_R                                      ~16     !1, !4
         23        IS_SMALLER                                               !0, ~16
         24      > JMPZ                                                     ~17, ->38
   15    25    >   ASSIGN                                                   !2, !4
   16    26        INIT_FCALL                                               'max'
         27        SEND_VAR                                                 !3
         28        DIV                                              ~19     !4, 2
         29        SEND_VAL                                                 ~19
         30        DO_ICALL                                         $20     
         31        CAST                                          4  ~21     $20
         32        ASSIGN                                                   !4, ~21
   17    33        INIT_FCALL                                               'printf'
         34        SEND_VAL                                                 'we%27re+too+high%2C+coming+down+to+%25d'
         35        SEND_VAR                                                 !4
         36        DO_ICALL                                                 
         37      > JMP                                                      ->52
   19    38    >   ASSIGN                                                   !3, !4
   20    39        INIT_FCALL                                               'min'
         40        SEND_VAR                                                 !2
         41        SUB                                              ~25     !2, !4
         42        DIV                                              ~26     ~25, 2
         43        ADD                                              ~27     !4, ~26
         44        SEND_VAL                                                 ~27
         45        DO_ICALL                                         $28     
         46        CAST                                          4  ~29     $28
         47        ASSIGN                                                   !4, ~29
   21    48        INIT_FCALL                                               'printf'
         49        SEND_VAL                                                 'we%27re+too+low%2C+increasing+to+%25d'
         50        SEND_VAR                                                 !4
         51        DO_ICALL                                                 
   23    52    >   IS_SMALLER                                       ~32     !4, !2
         53      > JMPZ_EX                                          ~32     ~32, ->57
         54    >   FETCH_DIM_R                                      ~33     !1, !4
         55        IS_NOT_IDENTICAL                                 ~34     !0, ~33
         56        BOOL                                             ~32     ~34
         57    > > JMPNZ                                                    ~32, ->10
   24    58    > > RETURN                                                   !4
   25    59*     > RETURN                                                   null

End of function binsearch

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
176.04 ms | 1403 KiB | 24 Q