# Two implementations of Game cards ordering written in PHP

This idea of this post is similar to my previous one. It's again for performance, but for a different task. This time, it's for cards ordering.

I wrote it in two different ways:

1. Using a predefined array with other I want.

2. Exploding cards value when I need to compare them.

Exploding cards value when I need to compare them.

You can find both implementations below, but just before then I want to share the significant performance difference between both ways based on the test I did.

The test was to execute 1000000 times ordering method with initial sets of cards order "4D", "4H", "4S", "4C", "10H" (after first iteration cards will be ordered and action will not change they order, but that is not the idea of the this post).

How I did the performance test.

I executed on brand new machine several times and I find out avarage result, which is:

Solution one - executed for

Solution two - executed for

Which makes first solution

The first way

The second way.

I wrote it in two different ways:

1. Using a predefined array with other I want.

2. Exploding cards value when I need to compare them.

Exploding cards value when I need to compare them.

You can find both implementations below, but just before then I want to share the significant performance difference between both ways based on the test I did.

The test was to execute 1000000 times ordering method with initial sets of cards order "4D", "4H", "4S", "4C", "10H" (after first iteration cards will be ordered and action will not change they order, but that is not the idea of the this post).

How I did the performance test.

$card = new Cards(); $card->setCards(["4D", "4H", "4S", "4C", "10H"]); $before = microtime(true); for ($i = 0; $i < 1000000; $i++) { $card->order(); } $after = microtime(true); echo ($after-$before);

I executed on brand new machine several times and I find out avarage result, which is:

Solution one - executed for

**1.491**seconds.Solution two - executed for

**4.237**seconds.Which makes first solution

**2.746**seconds faster, hence**~284.17%**.The first way

class Cards { private static $orderList = [ '2D' => 1, '2H' => 2, '2C' => 3, '2S' => 4, '3D' => 5, '3H' => 6, '3C' => 7, '3S' => 8, '4D' => 9, '4H' => 10, '4C' => 11, '4S' => 12, '5D' => 13, '5H' => 14, '5C' => 15, '5S' => 16, '6D' => 17, '6H' => 18, '6C' => 19, '6S' => 20, '7D' => 21, '7H' => 22, '7C' => 23, '7S' => 24, '8D' => 25, '8H' => 26, '8C' => 27, '8S' => 28, '9D' => 29, '9H' => 30, '9C' => 31, '9S' => 32, '10D' => 33, '10H' => 34, '10C' => 35, '10S' => 36, 'JD' => 37, 'JH' => 38, 'JC' => 39, 'JS' => 40, 'QD' => 41, 'QH' => 42, 'QC' => 43, 'QS' => 44, 'KD' => 45, 'KH' => 46, 'KC' => 47, 'KS' => 48, '1D' => 49, '1H' => 50, '1C' => 51, '1S' => 52, ]; private $cards = []; public function setCards(array $newCards) { $this->cards = $newCards; } public function getCards() { return $this->cards; } public function order() { uasort($this->cards, function($a, $b){ if (self::$orderList[$a] > self::$orderList[$b]) { return true; } return false; }); } }

The second way.

class Cards { private $cards = []; public function setCards(array $newCards) { $this->cards = $newCards; } public function getCards() { return $this->cards; } public function order() { uasort($this->cards, function($a, $b){ $aStrlen = strlen($a); $bStrlen = strlen($b); // basic validation if ($aStrlen > 1 && $aStrlen < 4 && $bStrlen > 1 && $bStrlen < 4) { // slipt "a" value if ($aStrlen == 3) { $aType = $a[2]; $aValue = substr($a, 0, 2); } else { $aType = $a[1]; $aValue = substr($a, 0, 1); } // slipt "b" value if ($bStrlen == 3) { $bType = $b[2]; $bValue = substr($b, 0, 2); } else { $bType = $b[1]; $bValue = substr($b, 0, 1); } if ($aValue > $bValue) { return true; } if ($aValue == $bValue) { if ($bType == "D") { return true; } if ($bType == "H" && ($aType == "C" || $aType =="S")) { return true; } if ($bType == "C" && $aType == "S"){ return true; } } } return false; }); } }