3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Block { public $nonce; public function __construct($index, $timestamp, $data, $previousHash = null) { $this->index = $index; $this->timestamp = $timestamp; $this->data = $data; $this->previousHash = $previousHash; $this->hash = $this->calculateHash(); $this->nonce = 0; } public function calculateHash() { return hash("sha256", $this->index.$this->previousHash.$this->timestamp.((string)$this->data).$this->nonce); } } class BlockChain { /** * Instantiates a new Blockchain. */ public function __construct() { $this->chain = [$this->createGenesisBlock()]; $this->difficulty = 4; } /** * Creates the genesis block. */ private function createGenesisBlock() { return new Block(0, strtotime("2017-01-01"), "Genesis Block"); } /** * Gets the last block of the chain. */ public function getLastBlock() { return $this->chain[count($this->chain)-1]; } /** * Pushes a new block onto the chain. */ public function push($block) { $block->previousHash = $this->getLastBlock()->hash; $this->mine($block); array_push($this->chain, $block); } /** * Mines a block. */ public function mine($block) { while (substr($block->hash, 0, $this->difficulty) !== str_repeat("0", $this->difficulty)) { $block->nonce++; $block->hash = $block->calculateHash(); } echo "Block mined: ".$block->hash."\n"; } /** * Validates the blockchain's integrity. True if the blockchain is valid, false otherwise. */ public function isValid() { for ($i = 1; $i < count($this->chain); $i++) { $currentBlock = $this->chain[$i]; $previousBlock = $this->chain[$i-1]; if ($currentBlock->hash != $currentBlock->calculateHash()) { return false; } if ($currentBlock->previousHash != $previousBlock->hash) { return false; } } return true; } } /* Set up a simple chain and mine two blocks. */ $testCoin = new BlockChain(); echo "mining block 1...\n"; $testCoin->push(new Block(1, strtotime("now"), "amount: 4")); echo "mining block 2...\n"; $testCoin->push(new Block(2, strtotime("now"), "amount: 10")); echo json_encode($testCoin, JSON_PRETTY_PRINT);

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)
8.4.20.0070.08822.52
8.4.10.0030.09424.12
8.3.150.0030.23917.32
8.3.140.0070.20217.04
8.3.130.0070.21816.97
8.3.120.0070.20419.22
8.3.110.0030.21016.93
8.3.100.0070.21920.91
8.3.90.0100.23516.97
8.3.80.0030.26117.97
8.3.70.0170.37918.62
8.3.60.0130.38918.68
8.3.50.0100.39417.01
8.3.40.0130.38620.42
8.3.30.0100.37618.92
8.3.20.0070.20524.18
8.3.10.0030.20524.66
8.3.00.0030.20426.16
8.2.260.0100.25017.17
8.2.250.0070.23018.79
8.2.240.0030.20817.18
8.2.230.0030.21720.94
8.2.220.0100.20024.06
8.2.210.0100.25926.77
8.2.200.0000.26816.88
8.2.190.0100.39316.75
8.2.180.0100.39516.75
8.2.170.0070.39319.21
8.2.160.0100.38422.96
8.2.150.0100.19825.66
8.2.140.0100.19724.66
8.2.130.0030.20926.16
8.2.120.0030.20520.08
8.2.110.0100.25022.08
8.2.100.0030.29217.97
8.2.90.0000.29018.00
8.2.80.0000.28617.97
8.2.70.0100.27818.15
8.2.60.0030.27619.21
8.2.50.0070.29018.10
8.2.40.0000.28420.42
8.2.30.0030.28518.13
8.2.20.0030.28319.32
8.2.10.0040.23817.96
8.2.00.0030.33918.00
8.1.310.0030.20616.92
8.1.300.0030.20720.18
8.1.290.0130.24418.88
8.1.280.0130.37625.92
8.1.270.0030.21820.65
8.1.260.0030.20426.35
8.1.250.0000.21128.09
8.1.240.0130.24119.40
8.1.230.0100.27220.96
8.1.220.0070.27517.74
8.1.210.0000.27818.92
8.1.200.0030.28017.38
8.1.190.0000.29417.48
8.1.180.0070.27718.10
8.1.170.0070.27318.71
8.1.160.0030.28519.04
8.1.150.0030.28618.86
8.1.140.0070.33117.68
8.1.130.0100.32317.59
8.1.120.0030.35717.70
8.1.110.0100.32117.62
8.1.100.0030.32317.77
8.1.90.0070.32417.75
8.1.80.0070.31817.75
8.1.70.0070.31717.73
8.1.60.0000.32717.84
8.1.50.0070.31617.73
8.1.40.0070.31917.70
8.1.30.0070.32117.69
8.1.20.0100.31917.75
8.1.10.0070.31917.64
8.1.00.0000.34017.74
8.0.300.0030.28719.90
8.0.290.0000.28517.00
8.0.280.0070.28018.58
8.0.270.0070.32817.11
8.0.260.0030.32516.91
8.0.250.0100.32516.95
8.0.240.0030.33117.02
8.0.230.0000.32517.10
8.0.220.0000.33516.92
8.0.210.0070.34917.05
8.0.200.0070.32917.11
8.0.190.0100.32517.12
8.0.180.0030.33317.09
8.0.170.0030.33717.05
8.0.160.0030.33017.02
8.0.150.0030.31916.96
8.0.140.0000.33316.96
8.0.130.0030.32816.96
8.0.120.0030.32017.08
8.0.110.0030.32417.04
8.0.100.0030.32117.07
8.0.90.0000.32917.04
8.0.80.0030.32616.94
8.0.70.0100.31916.96
8.0.60.0070.33016.95
8.0.50.0030.33116.95
8.0.30.0000.33616.95
8.0.20.0030.33417.02
8.0.10.0030.32217.06
7.4.330.0070.21416.30

preferences:
34.6 ms | 403 KiB | 5 Q