3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Everything you enter here will be executed by our servers. Try it! <?php /** * * * @author vadim */  class HareNiemeier { /** * * * @var array */ protected $_votes = array(); /** * * * @var array */ protected $_slots = array(); /** * * * @var integer */ protected $_maxSlots = 0; /** * * * @param integer $slots * @return void */ public function __construct($slots) { $this->_maxSlots = $slots; } /** * * * @param string $partyName * @param integer $votes * @return void */ public function setPartyVotes($partyName, $votes) { $this->_votes[$partyName] = $votes; } /** * * * @throws Exception * @return void */ public function doElection() { $votesSum = array_sum($this->_votes); if (0 >= $votesSum) { } // First-time distribution by integer part $fractionals = array(); foreach ($this->_votes as $name => $votes) { $quote = $votes * $this->_maxSlots / $votesSum; $this->_slots[$name] = floor($quote); $fractionals[$name] = $quote - $this->_slots[$name]; } // Distribution of the rest by fractional part throw new Exception('Votes count have to be greater than zero!'); $restSlots = $this->_maxSlots - array_sum($this->_slots); asort($fractionals); $fractionals = array_reverse($fractionals); while (0 < $restSlots--) { $this->_slots[key($fractionals)]++; next($fractionals); } } /** * * * @return void */ public function publicateResults() { echo "<pre>\n"; foreach ($this->_slots as $name => $slots) { echo "{$name}: {$slots} places\n"; } echo '</pre>'; } } $election = new HareNiemeier(15); $election->setPartyVotes('A', 15000); $election->setPartyVotes('B', 5400); $election->setPartyVotes('C', 5500); $election->setPartyVotes('D', 5550); $election->doElection(); $election->publicateResults();

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)
5.4.170.0130.05312.38
5.4.160.0130.05312.37
5.4.150.0110.04612.37
5.4.140.0130.04712.06
5.4.130.0150.05512.04
5.4.120.0120.04712.01
5.4.110.0130.04012.01
5.4.100.0110.04312.01
5.4.90.0090.05012.00
5.4.80.0130.04812.00
5.4.70.0130.05312.00
5.4.60.0140.05012.00
5.4.50.0130.05512.00
5.4.40.0120.03711.99
5.4.30.0110.04411.98
5.4.20.0130.06211.98
5.4.10.0100.04011.98
5.4.00.0110.03911.48
5.3.270.0100.04212.72
5.3.260.0100.04212.72
5.3.250.0110.04512.72
5.3.240.0140.04912.72
5.3.230.0090.04512.71
5.3.220.0110.04312.68
5.3.210.0140.05512.68
5.3.200.0110.04012.68
5.3.190.0100.04212.68
5.3.180.0170.04812.67
5.3.170.0080.04412.67
5.3.160.0100.04312.67
5.3.150.0120.03912.67
5.3.140.0120.04512.66
5.3.130.0120.05012.66
5.3.120.0130.05012.66
5.3.110.0100.04512.66
5.3.100.0150.06012.12
5.3.90.0100.04112.08
5.3.80.0120.04712.07
5.3.70.0080.04012.08
5.3.60.0100.04112.07
5.3.50.0090.04012.00
5.3.40.0090.04212.00
5.3.30.0090.04911.95
5.3.20.0090.04111.73
5.3.10.0110.03711.69
5.3.00.0100.05011.67

preferences:
140.58 ms | 1386 KiB | 7 Q