Skip to content

Instantly share code, notes, and snippets.

@tamakiii
Last active August 29, 2015 14:10
Show Gist options
  • Save tamakiii/325a987f53d01e2a38dd to your computer and use it in GitHub Desktop.
Save tamakiii/325a987f53d01e2a38dd to your computer and use it in GitHub Desktop.
<?php
/**
* $ curl -sS https://getcomposer.org/installer | php
* #!/usr/bin/env php
* All settings correct for using Composer
* Downloading...
*
* Composer successfully installed to: /Users/d-tamaki/tmp/php/pimple/composer.phar
* Use it: php composer.phar
*
* $ php composer.phar require 'pimple/pimple' '~3.0'
* ./composer.json has been updated
* Loading composer repositories with package information
* Updating dependencies (including require-dev)
* - Installing pimple/pimple (v3.0.0)
* Downloading: 100%
*
* - Installing monolog/monolog (1.2.1)
* Downloading: 100%
*
* monolog/monolog suggests installing mlehner/gelf-php (Allow sending log messages to a GrayLog2 server)
* monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
* monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)
* Writing lock file
* Generating autoload files
*
* $ php good.php
* $ php bad.php
*/
/*
$ php good.php
array(2) {
["sum"]=>
float(1.1425030231476)
["avg"]=>
float(0.00011426172848761)
}
$ php bad.php
array(2) {
["sum"]=>
float(3.0512113571167)
["avg"]=>
float(0.00030515165087676)
}
*/
?>
<?php
/* good.php */
require 'lib.php';
$c = new \Pimple\Container;
$alphabet = array_repeat(10, buildAlphabets());
foreach ($alphabet as $key => $a) {
$n = $key + 1;
$c[$key] = function($c) use ($a, $n) {
$next = $c[$n];
return function() use ($a, $next) {
return $a . $next();
};
};
}
$c[count($alphabet)] = function($c) {
return function() {
return '!';
};
};
var_dump(run($c));
?>
<?php
/* bad.php */
require 'lib.php';
$c = new \Pimple\Container;
$alphabet = array_repeat(10, buildAlphabets());
foreach ($alphabet as $key => $a) {
$n = $key + 1;
$c[$key] = function($c) use ($a, $n) {
return function() use ($a, $c, $n) {
return $a . $c[$n]();
};
};
}
$c[count($alphabet)] = function($c) {
return function() {
return '!';
};
};
var_dump(run($c));
?>
<?php
/* lib.php */
require __DIR__ . '/vendor/autoload.php';
function array_repeat($time, array $array)
{
$result = array();
for ($i = 0; $i < $time; ++$i) {
$result = array_merge($result, $array);
}
return $result;
}
function buildAlphabets()
{
return array_merge(range('A', 'Z'), range('a', 'z'));
}
function run(\Pimple\Container $c)
{
// Execute
$results = array();
$i = 0;
while (++$i < 10000) {
$start = microtime(true);
$c[0]();
$end = microtime(true);
$results[] = $end - $start;
}
$sum = array_sum($results);
$count = count($results);
return array(
'sum' => $sum,
'avg' => $sum/$count,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment