Skip to main content
added 2223 characters in body
Source Link
hakre
  • 199.8k
  • 55
  • 454
  • 866

Benchmark (via http://codepad.org/jqOQkaZR):

<?php require "autoload.php"; function convertStdToCollectionReflection(array $stds, $entity, $initVars) { $records = array(); $class = $entity . '\\Entity'; foreach ($stds as $record) { $args = array(); foreach ($initVars as $var) { $args[] = $record->$var; } $reflect = new \ReflectionClass($class); $records[] = $reflect->newInstanceArgs($args); } return $records; } function convertStdToCollectionSplat(array $stds, $entity, $initVars) { $records = array(); $class = $entity . '\\Entity'; foreach ($stds as $record) { $args = array(); foreach ($initVars as $var) { $args[] = $record->$var; } $records[] = new $class(...$args); } return $records; } $dummyObject = array(); for ($i = 0; $i < 10; $i++) { $dummyclass = new \stdClass(); $dummyclass->id = $i; $dummyclass->description = 'Just a number... ' . $i; $dummyObject[] = $dummyclass; } print 'Start Reflection test' . PHP_EOL; $reflectionStart = microtime(true); for($i = 0; $i < 1000000; $i++) { convertStdToCollectionReflection( $dummyObject, 'Xcs\Core\Record', array( 'id', 'description' ) ); } $reflectionEnd = microtime(true); print 'Start Splat test' . PHP_EOL; $splatStart = microtime(true); for($i = 0; $i < 1000000; $i++) { convertStdToCollectionSplat( $dummyObject, 'Xcs\Core\Record', array( 'id', 'description' ) ); } $splatEnd = microtime(true); print PHP_EOL . 'OUTPUT:' . PHP_EOL; print 'Reflection: ' . ($reflectionEnd - $reflectionStart) . 's' . PHP_EOL; print 'Splat: ' . ($splatEnd - $splatStart) . 's' . PHP_EOL; 

Benchmark (via http://codepad.org/jqOQkaZR):

<?php require "autoload.php"; function convertStdToCollectionReflection(array $stds, $entity, $initVars) { $records = array(); $class = $entity . '\\Entity'; foreach ($stds as $record) { $args = array(); foreach ($initVars as $var) { $args[] = $record->$var; } $reflect = new \ReflectionClass($class); $records[] = $reflect->newInstanceArgs($args); } return $records; } function convertStdToCollectionSplat(array $stds, $entity, $initVars) { $records = array(); $class = $entity . '\\Entity'; foreach ($stds as $record) { $args = array(); foreach ($initVars as $var) { $args[] = $record->$var; } $records[] = new $class(...$args); } return $records; } $dummyObject = array(); for ($i = 0; $i < 10; $i++) { $dummyclass = new \stdClass(); $dummyclass->id = $i; $dummyclass->description = 'Just a number... ' . $i; $dummyObject[] = $dummyclass; } print 'Start Reflection test' . PHP_EOL; $reflectionStart = microtime(true); for($i = 0; $i < 1000000; $i++) { convertStdToCollectionReflection( $dummyObject, 'Xcs\Core\Record', array( 'id', 'description' ) ); } $reflectionEnd = microtime(true); print 'Start Splat test' . PHP_EOL; $splatStart = microtime(true); for($i = 0; $i < 1000000; $i++) { convertStdToCollectionSplat( $dummyObject, 'Xcs\Core\Record', array( 'id', 'description' ) ); } $splatEnd = microtime(true); print PHP_EOL . 'OUTPUT:' . PHP_EOL; print 'Reflection: ' . ($reflectionEnd - $reflectionStart) . 's' . PHP_EOL; print 'Splat: ' . ($splatEnd - $splatStart) . 's' . PHP_EOL; 
Source Link
Blaatpraat
  • 2.8k
  • 13
  • 23

Today I've found the time to benchmark it.
And it's like I've expected (and Fleshgrinder said): the splat operator is faster.

Benchmark times:
Reflection: 11.686084032059s
Splat: 6.8125338554382s

Almost the half of time... That's serious...