Skip to content

Instantly share code, notes, and snippets.

@Majkl578
Created December 17, 2010 09:20
Show Gist options
  • Save Majkl578/744695 to your computer and use it in GitHub Desktop.
Save Majkl578/744695 to your computer and use it in GitHub Desktop.
static callback vs. create_function (for Lopo@nettechat)
<?php
class Foo
{
public static function testA($value, $prefix = '')
{
self::$substitutePrefix = $prefix;
return preg_replace_callback('#:([^:\s]*):#', array(__CLASS__, 'testCb'), $value);
}
private static $substitutePrefix; // workaround for f***ing PHP <5.3
private static function testCb($m)
{
return '';
}
public static function testB($value, $prefix = '')
{
return preg_replace_callback('#:([^:\s]*):#', create_function('$m, $prefix='."'$prefix'", 'return "";'), $value);
}
}
$xts = microtime(TRUE);
for ($i = 0; $i < 20000; $i++) {
Foo::testA(':foo:bar', 'x');
}
$xts = microtime(TRUE) - $xts;
$yts = microtime(TRUE);
for ($i = 0; $i < 20000; $i++) {
Foo::testB(':foo:bar', 'x');
}
$yts = microtime(TRUE) - $yts;
var_dump($xts, $yts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment