3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* Author: Silviu Schiau (@sschiau on Twitter) Web: www.schiau.co File: Particle.php Created: 1364036124000 (UNIX Time) Modified: 1378831167000 (UNIX Time) About: PHP implementation of Twitter Snowflake ID Generator (Extended to 42bits epoch for 96 years 1 month 21 days 16 hours 42 minutes 24 seconds in the future) License: Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt Thanks to Twitter for Snowflake. This header should NOT be removed if you want to use Particle. */ class Particle { const EPOCH=1378831167000; final public function generateParticle($machine_id) { //Time - 42 bits (millisecond precision w/ a custom epoch gives us 96 years 1 month 21 days 16 hours 42 minutes 24 seconds in the future) $time = floor(microtime(true) * 1000); //Substract custom epoch from current time $time -= self::EPOCH; //Add to base $base = pow(2,41); $base += $time; $base = decbin($base); //configured machine id - 10 bits - to 1024 machines $machineid = decbin($machine_id); //sequence number - 12 bits - up to 4096 random numbers per machine $random = mt_rand(0,pow(2,12)-1); $random = decbin($random); //Pack $base = $base.$machineid.$random; return base_convert($base,2,10); } final public function timeFromParticle($particle) { return base_convert(substr(base_convert($particle,10,2),0,42),2,10)-pow(2,41)+self::EPOCH; } } function getId($machine_id = 0) { $particle = new Particle; return $particle -> generateParticle($machine_id); } // We need to verify that there are no ID conflicts echo "Different machine same time\n"; echo getId(10) . "\n"; echo getId(20) . "\n"; echo getId(30) . "\n"; echo getId(10) . "\n"; echo getId(20) . "\n"; echo getId(30) . "\n"; echo "Same machine different time\n"; echo getId(1) . "\n"; echo getId(1) . "\n"; echo getId(1) . "\n"; echo getId(1) . "\n"; echo getId(1) . "\n"; echo getId(1) . "\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0MP6t
function name:  (null)
number of ops:  63
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   ECHO                                                     'Different+machine+same+time%0A'
   62     1        INIT_FCALL                                               'getid'
          2        SEND_VAL                                                 10
          3        DO_FCALL                                      0  $0      
          4        CONCAT                                           ~1      $0, '%0A'
          5        ECHO                                                     ~1
   63     6        INIT_FCALL                                               'getid'
          7        SEND_VAL                                                 20
          8        DO_FCALL                                      0  $2      
          9        CONCAT                                           ~3      $2, '%0A'
         10        ECHO                                                     ~3
   64    11        INIT_FCALL                                               'getid'
         12        SEND_VAL                                                 30
         13        DO_FCALL                                      0  $4      
         14        CONCAT                                           ~5      $4, '%0A'
         15        ECHO                                                     ~5
   65    16        INIT_FCALL                                               'getid'
         17        SEND_VAL                                                 10
         18        DO_FCALL                                      0  $6      
         19        CONCAT                                           ~7      $6, '%0A'
         20        ECHO                                                     ~7
   66    21        INIT_FCALL                                               'getid'
         22        SEND_VAL                                                 20
         23        DO_FCALL                                      0  $8      
         24        CONCAT                                           ~9      $8, '%0A'
         25        ECHO                                                     ~9
   67    26        INIT_FCALL                                               'getid'
         27        SEND_VAL                                                 30
         28        DO_FCALL                                      0  $10     
         29        CONCAT                                           ~11     $10, '%0A'
         30        ECHO                                                     ~11
   69    31        ECHO                                                     'Same+machine+different+time%0A'
   70    32        INIT_FCALL                                               'getid'
         33        SEND_VAL                                                 1
         34        DO_FCALL                                      0  $12     
         35        CONCAT                                           ~13     $12, '%0A'
         36        ECHO                                                     ~13
   71    37        INIT_FCALL                                               'getid'
         38        SEND_VAL                                                 1
         39        DO_FCALL                                      0  $14     
         40        CONCAT                                           ~15     $14, '%0A'
         41        ECHO                                                     ~15
   72    42        INIT_FCALL                                               'getid'
         43        SEND_VAL                                                 1
         44        DO_FCALL                                      0  $16     
         45        CONCAT                                           ~17     $16, '%0A'
         46        ECHO                                                     ~17
   73    47        INIT_FCALL                                               'getid'
         48        SEND_VAL                                                 1
         49        DO_FCALL                                      0  $18     
         50        CONCAT                                           ~19     $18, '%0A'
         51        ECHO                                                     ~19
   74    52        INIT_FCALL                                               'getid'
         53        SEND_VAL                                                 1
         54        DO_FCALL                                      0  $20     
         55        CONCAT                                           ~21     $20, '%0A'
         56        ECHO                                                     ~21
   75    57        INIT_FCALL                                               'getid'
         58        SEND_VAL                                                 1
         59        DO_FCALL                                      0  $22     
         60        CONCAT                                           ~23     $22, '%0A'
         61        ECHO                                                     ~23
         62      > RETURN                                                   1

Function getid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0MP6t
function name:  getId
number of ops:  9
compiled vars:  !0 = $machine_id, !1 = $particle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV_INIT                                        !0      0
   55     1        NEW                                              $2      'Particle'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $2
   56     4        INIT_METHOD_CALL                                         !1, 'generateParticle'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $5      
          7      > RETURN                                                   $5
   57     8*     > RETURN                                                   null

End of function getid

Class Particle:
Function generateparticle:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0MP6t
function name:  generateParticle
number of ops:  48
compiled vars:  !0 = $machine_id, !1 = $time, !2 = $base, !3 = $machineid, !4 = $random
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   22     1        INIT_FCALL                                               'floor'
          2        INIT_FCALL                                               'microtime'
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $5      
          5        MUL                                              ~6      $5, 1000
          6        SEND_VAL                                                 ~6
          7        DO_ICALL                                         $7      
          8        ASSIGN                                                   !1, $7
   25     9        ASSIGN_OP                                     2          !1, 1378831167000
   28    10        INIT_FCALL                                               'pow'
         11        SEND_VAL                                                 2
         12        SEND_VAL                                                 41
         13        DO_ICALL                                         $10     
         14        ASSIGN                                                   !2, $10
   29    15        ASSIGN_OP                                     1          !2, !1
   30    16        INIT_FCALL                                               'decbin'
         17        SEND_VAR                                                 !2
         18        DO_ICALL                                         $13     
         19        ASSIGN                                                   !2, $13
   33    20        INIT_FCALL                                               'decbin'
         21        SEND_VAR                                                 !0
         22        DO_ICALL                                         $15     
         23        ASSIGN                                                   !3, $15
   36    24        INIT_FCALL                                               'mt_rand'
         25        SEND_VAL                                                 0
         26        INIT_FCALL                                               'pow'
         27        SEND_VAL                                                 2
         28        SEND_VAL                                                 12
         29        DO_ICALL                                         $17     
         30        SUB                                              ~18     $17, 1
         31        SEND_VAL                                                 ~18
         32        DO_ICALL                                         $19     
         33        ASSIGN                                                   !4, $19
   37    34        INIT_FCALL                                               'decbin'
         35        SEND_VAR                                                 !4
         36        DO_ICALL                                         $21     
         37        ASSIGN                                                   !4, $21
   40    38        CONCAT                                           ~23     !2, !3
         39        CONCAT                                           ~24     ~23, !4
         40        ASSIGN                                                   !2, ~24
   42    41        INIT_FCALL                                               'base_convert'
         42        SEND_VAR                                                 !2
         43        SEND_VAL                                                 2
         44        SEND_VAL                                                 10
         45        DO_ICALL                                         $26     
         46      > RETURN                                                   $26
   43    47*     > RETURN                                                   null

End of function generateparticle

Function timefromparticle:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0MP6t
function name:  timeFromParticle
number of ops:  24
compiled vars:  !0 = $particle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
   47     1        INIT_FCALL                                               'base_convert'
          2        INIT_FCALL                                               'substr'
          3        INIT_FCALL                                               'base_convert'
          4        SEND_VAR                                                 !0
          5        SEND_VAL                                                 10
          6        SEND_VAL                                                 2
          7        DO_ICALL                                         $1      
          8        SEND_VAR                                                 $1
          9        SEND_VAL                                                 0
         10        SEND_VAL                                                 42
         11        DO_ICALL                                         $2      
         12        SEND_VAR                                                 $2
         13        SEND_VAL                                                 2
         14        SEND_VAL                                                 10
         15        DO_ICALL                                         $3      
         16        INIT_FCALL                                               'pow'
         17        SEND_VAL                                                 2
         18        SEND_VAL                                                 41
         19        DO_ICALL                                         $4      
         20        SUB                                              ~5      $3, $4
         21        ADD                                              ~6      ~5, 1378831167000
         22      > RETURN                                                   ~6
   48    23*     > RETURN                                                   null

End of function timefromparticle

End of class Particle.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.73 ms | 1402 KiB | 39 Q