Skip to content

Instantly share code, notes, and snippets.

@etki
Created February 22, 2015 16: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 etki/fabab58af38402988886 to your computer and use it in GitHub Desktop.
Save etki/fabab58af38402988886 to your computer and use it in GitHub Desktop.
Test
/composer
/composer.phar
/composer.lock
/vendor
.idea
.project
.settings
nbproject

How to run

  1. composer install
  2. bin/atletic -b vendor/autoload.php -p benchmarks
<?php
namespace Oh\Really\Benchmarks;
use Athletic\AthleticEvent;
use Oh\Really\PreparedRegexpAlgorithm;
use Oh\Really\PreparedStringAlgorithm;
use Oh\Really\RegexpAlgorithm;
use Oh\Really\StringAlgorithm;
/**
* Simple algorithm comparison
*
* @version 0.1.0
* @since 0.1.0
* @package Oh\Really\Benchmarks
* @author Etki <etki@etki.name>
*/
class AlgorithmComparison extends AthleticEvent
{
/**
* Regexp closure algorithm.
*
* @type RegexpAlgorithm
* @since 0.1.0
*/
private $regexpClosureAlgorithm;
/**
* Regexp standard-way-callback algorithm.
*
* @type PreparedRegexpAlgorithm
* @since 0.1.0
*/
private $regexpStandardAlgorithm;
/**
* String-function-based closure algorithm.
*
* @type StringAlgorithm
* @since 0.1.0
*/
private $stringClosureAlgorithm;
/**
* String-function-based standard-way-callback algorithm.
*
* @type PreparedStringAlgorithm
* @since 0.1.0
*/
private $stringStandardAlgorithm;
/**
* Test data.
*
* @type string[]
* @since 0.1.0
*/
private $data = array(
'keud orfd',
'signs %26 loves',
'love\'s and film',
'prosdfsd sdf',
'test',
);
/**
* Preparation method.
*
* @return void
* @since 0.1.0
*/
public function setUp()
{
$this->regexpClosureAlgorithm = new RegexpAlgorithm;
$this->regexpStandardAlgorithm = new PreparedRegexpAlgorithm;
$this->stringClosureAlgorithm = new StringAlgorithm;
$this->stringStandardAlgorithm = new PreparedStringAlgorithm;
}
/**
* @iterations 50000
*
* @return void
* @since 0.1.0
*/
public function regexpClosureAlgorithm()
{
$this->regexpClosureAlgorithm->run($this->data);
}
/**
* @iterations 50000
*
* @return void
* @since 0.1.0
*/
public function regexpStandardAlgorithm()
{
$this->regexpStandardAlgorithm->run($this->data);
}
/**
* @iterations 50000
*
* @return void
* @since 0.1.0
*/
public function stringClosureAlgorithm()
{
$this->stringClosureAlgorithm->run($this->data);
}
/**
* @iterations 50000
*
* @return void
* @since 0.1.0
*/
public function stringStandardAlgorithm()
{
$this->stringStandardAlgorithm->run($this->data);
}
}
{
"autoload": {
"psr-4": {
"Oh\\Really\\": "src/",
"Oh\\Really\\Benchmarks\\": "benchmarks/"
}
},
"require": {
"athletic/athletic": "~0.1"
},
"config": {
"bin-dir": "bin"
}
}
etki@vmint:~/Workspace/Playground/PHP/OhReally > vendor/bin/athletic -b vendor/autoload.php -p benchmarks
Oh\Really\Benchmarks\AlgorithmComparison
Method Name Iterations Average Time Ops/second
----------------------- ------------ -------------- -------------
regexpClosureAlgorithm : [50,000 ] [0.0000402507305] [24,844.26959]
regexpStandardAlgorithm: [50,000 ] [0.0000408648777] [24,470.89178]
stringClosureAlgorithm : [50,000 ] [0.0000350972319] [28,492.27551]
stringStandardAlgorithm: [50,000 ] [0.0000337328672] [29,644.67837]
<?php
namespace Oh\Really;
/**
* Basic interface.
*
* @version 0.1.0
* @since 0.1.0
* @package Oh\Really
* @author Etki <etki@etki.name>
*/
interface AlgorithmInterface
{
/**
* Runs algorithm
*
* @param string[] $lines Raw data (list of lines).
*
* @return string[] Processed data.
* @since 0.1.0
*/
public function run(array $lines);
}
<?php
namespace Oh\Really;
/**
* Regular expression-based algorithm with traditional callback.
*
* @version 0.1.0
* @since 0.1.0
* @package Oh\Really
* @author Etki <etki@etki.name>
*/
class PreparedRegexpAlgorithm implements AlgorithmInterface
{
/**
* {@inheritdoc}
*
* @param string[] $lines Raw input lines.
*
* @return string[] Processed lines.
* @since 0.1.0
*/
public function run(array $lines)
{
return array_filter($lines, array($this, 'callback',));
}
/**
* Callback for array filtering.
*
* @param string $line Line to be analyzed.
*
* @return int
* @since 0.1.0
*/
public function callback($line)
{
return (bool) preg_match('/[%$@]+/', $line);
}
}
<?php
namespace Oh\Really;
/**
* Simple algo that doesn't use closures and supposed to be faster.
*
* @version 0.1.0
* @since 0.1.0
* @package Oh\Really
* @author Etki <etki@etki.name>
*/
class PreparedStringAlgorithm implements AlgorithmInterface
{
/**
* {@inheritdoc}
*
* @param string[] $lines Raw input lines.
*
* @return string[] Processed lines.
* @since 0.1.0
*/
public function run(array $lines)
{
return array_filter($lines, array($this, 'callback',));
}
/**
* Callback fro calling array_filter().
*
* @param string $line Line to analyze.
*
* @return bool
* @since 0.1.0
*/
public function callback($line)
{
return strpbrk($line, '%$@') === false;
}
}
<?php
namespace Oh\Really;
/**
* Regular expression-based algorithm with closure.
*
* @version 0.1.0
* @since 0.1.0
* @package Oh\Really
* @author Etki <etki@etki.name>
*/
class RegexpAlgorithm implements AlgorithmInterface
{
/**
* {@inheritdoc}
*
* @param string[] $lines Raw input lines.
*
* @return string[] Processed lines.
* @since 0.1.0
*/
public function run(array $lines)
{
$callback = function ($line) {
return preg_match('/[%$@]+/', $line);
};
return array_filter($lines, $callback);
}
}
<?php
namespace Oh\Really;
/**
* Bare string-function-based filtering algorithm.
*
* @version 0.1.0
* @since 0.1.0
* @package Oh\Really
* @author Etki <etki@etki.name>
*/
class StringAlgorithm implements AlgorithmInterface
{
/**
* {@inheritdoc}
*
* @param string[] $lines Raw input lines.
*
* @return string[] Processed lines.
* @since 0.1.0
*/
public function run(array $lines)
{
$callback = function ($line) {
return strpbrk($line, '%$@') === false;
};
return array_filter($lines, $callback);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment