From f102cd14ce7a999096a532f19f5a8331fa241b2b Mon Sep 17 00:00:00 2001 From: Nat Zimmermann Date: Thu, 24 Oct 2019 11:39:38 +0100 Subject: [PATCH 1/4] optimise checkType by reducing function calls --- src/Collection.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Collection.php b/src/Collection.php index 3fdc871..6ce414f 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -118,25 +118,26 @@ public function offsetSet($index, $value) * value if it is. * * @param mixed $value - * @param string|null $type + * @param string|null $expectedType * @return mixed * @throws TypeError */ - private function checkType($value, ?string $type) + private function checkType($value, ?string $expectedType) { - if ($type) { - if (is_object($value) && !$value instanceof $type) { + if ($expectedType) { + $valueType = gettype($value); + if ($valueType === 'object' && !$value instanceof $expectedType) { throw new TypeError(sprintf( "Invalid object type. Should be %s: %s collected", - $type, + $expectedType, get_class($value) )); } - if (!is_object($value) && gettype($value) !== $type) { + if ($valueType !== 'object' && $valueType !== $expectedType) { throw new TypeError(sprintf( "Invalid type. Should be %s: %s collected", - $type, - gettype($value) + $expectedType, + $valueType )); } } From 3dd3e884983e3d1262ff30861d18dcba5c04ab6e Mon Sep 17 00:00:00 2001 From: Nat Zimmermann Date: Thu, 24 Oct 2019 13:20:37 +0100 Subject: [PATCH 2/4] add performance script --- performance.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++ src/Collection.php | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 performance.php diff --git a/performance.php b/performance.php new file mode 100644 index 0000000..4c1efe7 --- /dev/null +++ b/performance.php @@ -0,0 +1,50 @@ +append($a); +} + +$end = microtime(true); + +$duration = $end - $start; + +echo "Time: $duration\n"; + +class BoolCollection extends \Managur\Collection\Collection +{ + protected $valueType = 'boolean'; +} + +$collection = new BoolCollection(); + +echo "Scalar performance\n"; + +$start = microtime(true); + +for ($i = 0; $i < 5000000; ++$i) { + $collection->append(true); +} + +$end = microtime(true); + +$duration = $end - $start; + +echo "Time: $duration\n"; diff --git a/src/Collection.php b/src/Collection.php index 6ce414f..b480e5e 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -125,7 +125,7 @@ public function offsetSet($index, $value) private function checkType($value, ?string $expectedType) { if ($expectedType) { - $valueType = gettype($value); + $valueType = \gettype($value); if ($valueType === 'object' && !$value instanceof $expectedType) { throw new TypeError(sprintf( "Invalid object type. Should be %s: %s collected", From 37b9a819b9df76cf0ee3badda67fc3f50717e3a5 Mon Sep 17 00:00:00 2001 From: Nat Zimmermann Date: Thu, 24 Oct 2019 13:22:46 +0100 Subject: [PATCH 3/4] more performance --- src/Collection.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Collection.php b/src/Collection.php index b480e5e..59db0fe 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -126,14 +126,15 @@ private function checkType($value, ?string $expectedType) { if ($expectedType) { $valueType = \gettype($value); - if ($valueType === 'object' && !$value instanceof $expectedType) { - throw new TypeError(sprintf( - "Invalid object type. Should be %s: %s collected", - $expectedType, - get_class($value) - )); - } - if ($valueType !== 'object' && $valueType !== $expectedType) { + if ($valueType === 'object') { + if (!$value instanceof $expectedType) { + throw new TypeError(sprintf( + "Invalid object type. Should be %s: %s collected", + $expectedType, + get_class($value) + )); + } + } elseif ($valueType !== $expectedType) { throw new TypeError(sprintf( "Invalid type. Should be %s: %s collected", $expectedType, From 5a662b584c2b2d0ab79b2d6bc28113a593a0a4d7 Mon Sep 17 00:00:00 2001 From: Nat Zimmermann Date: Thu, 24 Oct 2019 13:28:21 +0100 Subject: [PATCH 4/4] remove script --- performance.php | 50 ------------------------------------------------- 1 file changed, 50 deletions(-) delete mode 100644 performance.php diff --git a/performance.php b/performance.php deleted file mode 100644 index 4c1efe7..0000000 --- a/performance.php +++ /dev/null @@ -1,50 +0,0 @@ -append($a); -} - -$end = microtime(true); - -$duration = $end - $start; - -echo "Time: $duration\n"; - -class BoolCollection extends \Managur\Collection\Collection -{ - protected $valueType = 'boolean'; -} - -$collection = new BoolCollection(); - -echo "Scalar performance\n"; - -$start = microtime(true); - -for ($i = 0; $i < 5000000; ++$i) { - $collection->append(true); -} - -$end = microtime(true); - -$duration = $end - $start; - -echo "Time: $duration\n";