Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Laravel enterprise level code quality

Name: Anonymous 2014-06-01 1:30

Laravel enterprise level code quality


public static function __callStatic($method, $args)
{
$instance = static::getFacadeRoot();

switch (count($args))
{
case 0:
return $instance->$method();

case 1:
return $instance->$method($args[0]);

case 2:
return $instance->$method($args[0], $args[1]);

case 3:
return $instance->$method($args[0], $args[1], $args[2]);

case 4:
return $instance->$method($args[0], $args[1], $args[2], $args[3]);

default:
return call_user_func_array(array($instance, $method), $args);
}
}

Name: Anonymous 2014-06-01 4:41

>>4
Here is a benchmark to prove my case:


<?php

class Bar {

public function addNumbers($a, $b)
{
return $a + $b;
}

}

class Foo {

private static $rootInstance;

protected static function getFacadeRoot()
{
if(is_null(self::$rootInstance))
self::$rootInstance = new Bar;

return self::$rootInstance;
}

}

class Unoptimized extends Foo {

public static function __callStatic($method, $args)
{
$instance = static::getFacadeRoot();
return call_user_func_array(array($instance, $method), $args);
}

}

class Optimized extends Foo {

public static function __callStatic($method, $args)
{
$instance = static::getFacadeRoot();

switch (count($args))
{
case 0:
return $instance->$method();

case 1:
return $instance->$method($args[0]);

case 2:
return $instance->$method($args[0], $args[1]);

case 3:
return $instance->$method($args[0], $args[1], $args[2]);

case 4:
return $instance->$method($args[0], $args[1], $args[2], $args[3]);

default:
return call_user_func_array(array($instance, $method), $args);
}
}

}

// instantiate the instance with a static call to make sure the comparison is fair
Optimized::addNumbers(1, 2);

$c = 1000000;
$t = microtime(true);

for($i = 0; $i < $c; ++$i)
Unoptimized::addNumbers(1, 2);

$t = microtime(true) - $t;
echo 'Unoptimized: ' . $t . PHP_EOL;
$t = microtime(true);

for($i = 0; $i < $c; ++$i)
Optimized::addNumbers(1, 2);

$t = microtime(true) - $t;
echo 'Optimized: ' . $t . PHP_EOL;


Output:
Unoptimized: 2.942752122879
Optimized: 2.2685830593109

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List