mixin.php 1.33 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
<?php
function mixin($tpl, $method = null)
{
    static $_map;
    
    if ( ! isset($_map[$tpl . $method])) {
        if ($method === null) {
            $refl = new ReflectionFunction($tpl);
        } else {
            $refl = new ReflectionMethod($tpl, $method);
        }
    
        $lines = file($refl->getFileName());
    
        $start = $refl->getStartLine();
        $end   = $refl->getEndLine();
    
        $ret = array_slice($lines, $start, ($end - $start));
    
        $code = trim(trim(implode(' ', $ret)), '{}');

        $_map[$tpl . $method] = $code;
    } else {
        $code = $_map[$tpl . $method];      	
    }
    eval($code);
}
function someCode() {
    $a = 10;
}
class Template 
{
    public function exec()
    {
        $a = 10;
    }
}
print "<pre>MIXIN BENCHMARK \n";

$timepoint = microtime(true);

$i = 500;

while ($i--) {
    mixin('someCode');
}

print 'EXECUTED 500 CODE BLOCKS : ' . (microtime(true) - $timepoint) . "\n";


$timepoint = microtime(true);

$i = 500;

while ($i--) {
    someCode();
}

print 'EXECUTED 500 DIRECT FUNCTION CALLS : ' . (microtime(true) - $timepoint) . "\n";

$timepoint = microtime(true);

$i = 500;

while ($i--) {
    eval('$a = 10;');
}

print 'EXECUTED 500 DIRECT EVAL CALLS : ' . (microtime(true) - $timepoint) . "\n";