Skip to content

[Improve] use token_get_all instead of tokenize method #15

@tinpont

Description

@tinpont

Thanks for your awesome project.

Can we improve performance by replacing tokenize to token_get_all ?

tinpont@local:~/expression-test$ php -f compare.php
token_get_all: 0.077315092086792
StringCalc: 2.0649530887604

Source code:

<?php

require 'vendor/autoload.php';

$limit = 100000;
$expression = 'INT(W*0.013)*0.014';
$startTime = microtime(true);
for ($i = 0; $i < $limit; $i ++) {
    $tokens = token_get_all("<?php $expression;");
}
echo 'token_get_all: ' . (microtime(true) - $startTime) . PHP_EOL;

$stringCalc = new ChrisKonnertz\StringCalc\StringCalc();
$startTime = microtime(true);
for ($i = 0; $i < $limit; $i ++) {
    $tokens = $stringCalc->tokenize($expression);
}
echo 'StringCalc: ' . (microtime(true) - $startTime) . PHP_EOL;

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions