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,40); $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,11)-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,41),2,10)-pow(2,41)+self::EPOCH; } } function getId($machine_id = 0, $sleep = true) { // Without sleeping 1ms two elements created at the very // same time will collide when generated on the same machine if ($sleep) { usleep(1000); } $particle = new Particle; return $particle->generateParticle($machine_id); } // We need to verify that there are no ID conflicts echo "Same machine same time\n"; echo getId(1, false) . "\n"; echo getId(1, false) . "\n"; echo getId(1, false) . "\n"; echo getId(1, false) . "\n"; echo getId(1, false) . "\n"; echo getId(1, false) . "\n"; echo "Different machine same time\n"; echo getId(10, false) . "\n"; echo getId(20, false) . "\n"; echo getId(30, false) . "\n"; echo getId(10, false) . "\n"; echo getId(20, false) . "\n"; echo getId(30, false) . "\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";

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
7.3.10.0090.00616.23
7.3.00.0060.00616.66
7.2.130.0080.00816.82
7.2.120.0040.01216.94
7.2.110.0030.01617.07
7.2.100.0190.00316.73
7.2.90.0090.01116.59
7.2.80.0200.00716.67
7.2.70.0040.01516.92
7.2.60.0040.00816.50
7.2.50.0220.00416.52
7.2.40.0270.00416.86
7.2.30.0280.00316.57
7.2.20.0100.00716.42
7.2.10.0220.00416.84
7.2.00.0080.01118.08
7.1.250.0090.01315.45
7.1.70.0040.01217.10
7.1.60.0040.01219.82
7.1.50.0080.01417.10
7.1.00.0030.07322.30
7.0.200.0000.01116.68
7.0.140.0070.07321.93
7.0.60.0070.08719.84
7.0.50.0130.08317.91
7.0.40.0130.08719.96
7.0.30.0300.05020.16
7.0.20.0200.04320.16
7.0.10.0330.08020.07
7.0.00.0030.04020.34
5.6.280.0070.07020.89
5.6.210.0070.04020.72
5.6.200.0070.08718.16
5.6.190.0130.08320.64
5.6.180.2170.03720.33
5.6.170.0230.06720.51
5.6.160.0070.04320.43
5.6.150.0130.07018.13
5.6.140.0030.03718.24
5.6.130.0030.04018.21
5.6.120.0100.07721.12
5.6.110.0070.04320.91
5.6.100.0030.08721.01
5.6.90.0000.04720.99
5.6.80.0070.05320.54
5.6.70.0300.07020.37
5.5.350.0100.07720.41
5.5.340.0030.06018.02
5.5.330.0130.05320.43
5.5.320.0270.05020.20
5.5.310.0270.05020.50
5.5.300.0070.04718.07
5.5.290.0100.03718.11
5.5.280.0030.05320.68
5.5.270.0130.08320.75
5.5.260.0070.05320.91
5.5.250.0100.07720.71
5.5.240.0130.03320.18
5.4.450.0330.07019.20
5.4.440.0070.06319.16
5.4.430.0100.05719.44
5.4.420.0100.05719.32
5.4.410.0300.03719.32
5.4.400.0630.00018.87
5.4.390.0830.00018.60
5.4.380.0230.05718.55
5.4.370.0200.06018.55
5.4.360.0330.07318.82
5.4.350.0430.05718.76
5.4.340.0300.04318.80
5.4.320.0110.03712.53
5.4.310.0080.03912.52
5.4.300.0100.03312.53
5.4.290.0070.05112.53
5.4.280.0080.03512.43
5.4.270.0080.03412.42
5.4.260.0070.05718.82
5.4.250.0030.06019.00
5.4.240.0100.05319.02
5.4.230.0100.05718.98
5.4.220.0030.06018.93
5.4.210.0030.05718.96
5.4.200.0170.05018.80
5.4.190.0130.04719.00
5.4.180.0130.05319.01
5.4.170.0200.05718.98
5.4.160.0200.04318.95
5.4.150.0200.06718.95
5.4.140.0070.07716.45
5.4.130.0030.07316.45
5.4.120.0130.05016.40
5.4.110.0070.08316.41
5.4.100.0100.05016.67
5.4.90.0200.05016.62
5.4.80.0100.07316.51
5.4.70.0170.06316.65
5.4.60.0100.06016.48
5.4.50.0170.06716.32
5.4.40.0170.05316.50
5.4.30.0100.05316.48
5.4.20.0030.05716.44
5.4.10.0130.07316.62
5.4.00.0100.06715.93
5.3.290.0050.05112.80
5.3.280.0070.05314.84
5.3.270.0200.07014.61
5.3.260.0070.05714.59
5.3.250.0130.04714.80
5.3.240.0030.06714.76
5.3.230.0130.05314.45
5.3.220.0130.05714.67
5.3.210.0130.05014.59
5.3.200.0070.05314.68
5.3.190.0070.05714.72
5.3.180.0030.05714.59
5.3.170.0230.06314.41
5.3.160.0070.05314.41
5.3.150.0130.05014.57
5.3.140.0170.06314.56
5.3.130.0130.04714.79
5.3.120.0130.05714.63
5.3.110.0100.05314.75
5.3.100.0170.05314.02
5.3.90.0100.07314.13
5.3.80.0100.07014.02
5.3.70.0030.05713.87
5.3.60.0170.06014.00
5.3.50.0000.05714.19
5.3.40.0070.05313.88
5.3.30.0070.07313.97
5.3.20.0100.05713.80
5.3.10.0100.06013.75
5.3.00.0030.07013.84
5.2.170.0030.04311.16
5.2.160.0130.05311.12
5.2.150.0130.04311.17
5.2.140.0170.03711.12
5.2.130.0130.04711.11
5.2.120.0070.06011.11
5.2.110.0300.02711.08
5.2.100.0100.05011.13
5.2.90.0030.06011.28
5.2.80.0100.06011.01
5.2.70.0030.04311.01
5.2.60.0000.06711.14
5.2.50.0100.04311.27
5.2.40.0130.05011.11
5.2.30.0070.06011.07
5.2.20.0070.04011.07
5.2.10.0100.04010.96
5.2.00.0100.05010.81
5.1.60.0170.0409.98
5.1.50.0100.03310.07
5.1.40.0070.0409.99
5.1.30.0030.04010.44
5.1.20.0000.04310.46
5.1.10.0000.04010.14
5.1.00.0070.04010.16
5.0.50.0070.0408.77
5.0.40.0070.0408.64
5.0.30.0070.0538.23
5.0.20.0030.0278.42
5.0.10.0070.0408.17
5.0.00.0030.0478.23
4.4.90.0000.0276.21
4.4.80.0070.0236.21
4.4.70.0000.0336.21
4.4.60.0070.0206.21
4.4.50.0030.0206.21
4.4.40.0030.0406.21
4.4.30.0070.0206.21
4.4.20.0000.0276.21
4.4.10.0000.0276.56
4.4.00.0030.0376.56
4.3.110.0070.0276.56
4.3.100.0000.0276.56
4.3.90.0000.0336.56
4.3.80.0030.0336.56
4.3.70.0030.0306.56
4.3.60.0070.0276.56
4.3.50.0000.0276.56
4.3.40.0070.0406.56
4.3.30.0000.0336.56
4.3.20.0030.0206.56
4.3.10.0070.0176.56
4.3.00.0130.0239.26

preferences:
30.94 ms | 400 KiB | 5 Q