Skip to content

Instantly share code, notes, and snippets.

@fabpot
Last active December 22, 2015 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabpot/6462069 to your computer and use it in GitHub Desktop.
Save fabpot/6462069 to your computer and use it in GitHub Desktop.
<?php
require_once __DIR__.'/autoload.php.dist';
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\ExpressionLanguage\Lexer;
use Symfony\Component\ExpressionLanguage\Parser;
use Symfony\Component\ExpressionLanguage\Compiler;
use Symfony\Component\ExpressionLanguage\SerializedParsedExpression;
class Obj
{
public $foo = 'bar';
public function bar($foo)
{
return $foo;
}
}
$el = new ExpressionLanguage();
$context = array('number' => 12, 'foo' => 'bar', 'arr' => array('foo' => 'bar'), 'obj' => new Obj());
$nb = 1000;
$lexer = new Lexer();
$parser = new Parser(array());
$compiler = new Compiler(array());
foreach (array('simple', 'complex') as $type) {
print "Internal Benchmarks ($type)\n";
print "-------------------\n\n";
$a = microtime(true);
for ($i = 0; $i < $nb; $i++) {
$lexer->tokenize(get_expression($type));
}
printf("LEXER %.3fms\n", (microtime(true) - $a) * 1000 / $nb);
$tokens = $lexer->tokenize(get_expression($type));
$a = microtime(true);
for ($i = 0; $i < $nb; $i++) {
$parser->parse(clone $tokens, array_keys($context));
}
printf("PARSER %.3fms\n", (microtime(true) - $a) * 1000 / $nb);
$nodes = $parser->parse(clone $tokens, array_keys($context));
$a = microtime(true);
for ($i = 0; $i < $nb; $i++) {
$compiler->compile($nodes);
}
printf("COMPILER %.3fms\n", (microtime(true) - $a) * 1000 / $nb);
print "\nEvaluation Benchmarks ($type)\n";
print "---------------------\n\n";
$a = microtime(true);
for ($i = 0; $i < $nb; $i++) {
$el->evaluate(get_expression($type), $context);
}
printf("EVAL %.3fms (from string expression)\n", (microtime(true) - $a) * 1000 / $nb);
$expr = get_expression($type);
$expr = new SerializedParsedExpression($expr, serialize($el->parse($expr, array_keys($context))));
$a = microtime(true);
for ($i = 0; $i < $nb; $i++) {
$el->evaluate($expr, $context);
}
printf("EVAL %.3fms (from serialized expression)\n\n", (microtime(true) - $a) * 1000 / $nb);
}
function get_expression($type)
{
// we put some random string to avoid hitting our internal cache
if ('simple' == $type) {
// simple expression
return 'obj.bar("foo") == "bar"';
}
// a more complex expression (you will probably never have anything more complex)
return sprintf('arr["foo"] ~ (%d + number) ? number : obj.bar("foo")', mt_rand());
}
Results are in ms.
Internal Benchmarks (simple)
-------------------
LEXER 0.130ms
PARSER 0.126ms
COMPILER 0.070ms
Evaluation Benchmarks (simple)
---------------------
EVAL 0.039ms (from string expression)
EVAL 0.056ms (from serialized expression)
Internal Benchmarks (complex)
-------------------
LEXER 0.309ms
PARSER 0.290ms
COMPILER 0.137ms
Evaluation Benchmarks (complex)
---------------------
EVAL 0.634ms (from string expression)
EVAL 0.070ms (from serialized expression)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment