Skip to content

Instantly share code, notes, and snippets.

@Maikuolan
Last active February 14, 2016 22:07
Show Gist options
  • Save Maikuolan/81774db2044898772de9 to your computer and use it in GitHub Desktop.
Save Maikuolan/81774db2044898772de9 to your computer and use it in GitHub Desktop.
<?php
function time_diff($a,$b) {
$a = explode(' ', $a, 2);
$b = explode(' ', $b, 2);
return ($b[1] + $b[0]) - ($a[1] + $a[0]);
}
/**
* extends the keys of $pairs to replaceable selectors
*
* ToDo: flag to @api if it works properly
*
* @author Matthias Kaschubowski
* @param string[] $pairs
* @return string[]
*/
function phpMussel_incubatePairs(array $pairs) {
return array_combine(
array_map('phpMussel_normalizeKeySelector', array_keys($pairs)),
array_values($pairs)
);
}
/**
* normalizes a string as a replaceable selector
*
* ToDo: flag to @api if it works properly
*
* @author Matthias Kaschubowski
* @param string $string
* @return string
*/
function phpMussel_normalizeKeySelector($string) {
return sprintf('{%s}', trim($string, '{}'));
}
class Test1
{
function phpMusselV($v, $b) {
if (!is_array($v) || empty($b)) {
return '';
}
/**
* Changed logic: For testing purposes
* @author Matthias Kaschubowski
*/
return strtr($b, phpMussel_incubatePairs($v));
}
}
class Test2
{
function phpMusselV($v, $b) {
if (!is_array($v) || empty($b)) {
return '';
}
$c = count($v);
reset($v);
for ($i = 0; $i < $c; $i++) {
$k = key($v);
$b = str_replace('{' . $k . '}', $v[$k], $b);
next($v);
}
return $b;
}
}
for($a = 0; $a < 10; $a++) {
echo "\n\nIteration ".$a.".\n\n";
$t = microtime();
for($i = 0; $i < 100000; $i++) {
$x = new Test1;
$x = $x -> phpMusselV(array('a' => 'AAA','b' => 'BBB','c' => 'CCC','d' => 'DDD','e' => 'EEE'), 'x {a} y {b} w {c} t {d} q {e} z');
}
echo 'Time (Test1): '.time_diff($t,microtime()). "seconds.\n\n";
$t = microtime();
for($i = 0; $i < 100000; $i++) {
$x = new Test2;
$x = $x -> phpMusselV(array('a' => 'AAA','b' => 'BBB','c' => 'CCC','d' => 'DDD','e' => 'EEE'), 'x {a} y {b} w {c} t {d} q {e} z');
}
echo 'Time (Test2): '.time_diff($t,microtime()). "seconds.\n\n";
}
/* Results:
Iteration 0.
Time (Test1): 0.39000010490417seconds.
Time (Test2): 0.24960088729858seconds.
Iteration 1.
Time (Test1): 0.39000010490417seconds.
Time (Test2): 0.24960088729858seconds.
Iteration 2.
Time (Test1): 0.39000010490417seconds.
Time (Test2): 0.24960088729858seconds.
Iteration 3.
Time (Test1): 0.40560102462769seconds.
Time (Test2): 0.23399996757507seconds.
Iteration 4.
Time (Test1): 0.40560102462769seconds.
Time (Test2): 0.24959993362427seconds.
Iteration 5.
Time (Test1): 0.39000105857849seconds.
Time (Test2): 0.24959993362427seconds.
Iteration 6.
Time (Test1): 0.39000105857849seconds.
Time (Test2): 0.24959993362427seconds.
Iteration 7.
Time (Test1): 0.40560102462769seconds.
Time (Test2): 0.24960112571716seconds.
Iteration 8.
Time (Test1): 0.3899998664856seconds.
Time (Test2): 0.23400115966797seconds.
Iteration 9.
Time (Test1): 0.40560007095337seconds.
Time (Test2): 0.24960088729858seconds.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment